diff options
author | Thomas Haller <thaller@redhat.com> | 2017-03-30 14:56:19 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2017-03-30 14:56:21 +0200 |
commit | 29bcfc25221fcbc63ce9bc7670097b07b7fab827 (patch) | |
tree | c1f38ed28645fde792dfa8567ede0360c1c41329 | |
parent | 85acdd70e7daba3257a3811c819a5f9a243bdfa6 (diff) | |
download | NetworkManager-29bcfc25221fcbc63ce9bc7670097b07b7fab827.tar.gz |
cli: don't track output data in global NmCli structure
We should not violate the global data to track the output data
while it is constructed and printed.
Most of the time, we actually clear the output data anyway --
either before constructing it, or after printing it.
In some cases we didn't, but I think that is a bug. It's really
hard to keep track of this.
The output data should belong to a certain scope and get destroyed
afterwards. Passing it around is very confusing. Don't do that.
-rw-r--r-- | clients/cli/common.c | 64 | ||||
-rw-r--r-- | clients/cli/common.h | 8 | ||||
-rw-r--r-- | clients/cli/connections.c | 101 | ||||
-rw-r--r-- | clients/cli/devices.c | 302 | ||||
-rw-r--r-- | clients/cli/general.c | 39 | ||||
-rw-r--r-- | clients/cli/nmcli.c | 4 | ||||
-rw-r--r-- | clients/cli/nmcli.h | 8 | ||||
-rw-r--r-- | clients/cli/settings.c | 11 |
8 files changed, 283 insertions, 254 deletions
diff --git a/clients/cli/common.c b/clients/cli/common.c index ef4a2cb34a..c17bc8194a 100644 --- a/clients/cli/common.c +++ b/clients/cli/common.c @@ -83,7 +83,7 @@ NmcOutputField nmc_fields_dhcp6_config[] = { gboolean print_ip4_config (NMIPConfig *cfg4, - NmCli *nmc, + const NmcConfig *nmc_config, const char *group_prefix, const char *one_field) { @@ -96,16 +96,17 @@ print_ip4_config (NMIPConfig *cfg4, int i = 0; NmcOutputField *tmpl, *arr; size_t tmpl_len; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); if (cfg4 == NULL) return FALSE; tmpl = nmc_fields_ip4_config; tmpl_len = sizeof (nmc_fields_ip4_config); - nmc->out.print_fields.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_IP4_CONFIG_ALL, + out.print_fields.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_IP4_CONFIG_ALL, tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); /* addresses */ ptr_array = nm_ip_config_get_addresses (cfg4); @@ -160,19 +161,17 @@ print_ip4_config (NMIPConfig *cfg4, set_val_arr (arr, 4, dns_arr); set_val_arr (arr, 5, domain_arr); set_val_arr (arr, 6, wins_arr); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); - - nmc_empty_output_fields (&nmc->out); + print_data_prepare_width (out.output_data); + print_data (nmc_config, &out); return TRUE; } gboolean print_ip6_config (NMIPConfig *cfg6, - NmCli *nmc, + const NmcConfig *nmc_config, const char *group_prefix, const char *one_field) { @@ -184,16 +183,17 @@ print_ip6_config (NMIPConfig *cfg6, int i = 0; NmcOutputField *tmpl, *arr; size_t tmpl_len; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); if (cfg6 == NULL) return FALSE; tmpl = nmc_fields_ip6_config; tmpl_len = sizeof (nmc_fields_ip6_config); - nmc->out.print_fields.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_IP6_CONFIG_ALL, + out.print_fields.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_IP6_CONFIG_ALL, tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); /* addresses */ ptr_array = nm_ip_config_get_addresses (cfg6); @@ -244,19 +244,17 @@ print_ip6_config (NMIPConfig *cfg6, set_val_arr (arr, 3, route_arr); set_val_arr (arr, 4, dns_arr); set_val_arr (arr, 5, domain_arr); - g_ptr_array_add (nmc->out.output_data, arr); - - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + g_ptr_array_add (out.output_data, arr); - nmc_empty_output_fields (&nmc->out); + print_data_prepare_width (out.output_data); + print_data (nmc_config, &out); return TRUE; } gboolean print_dhcp4_config (NMDhcpConfig *dhcp4, - NmCli *nmc, + const NmcConfig *nmc_config, const char *group_prefix, const char *one_field) { @@ -273,13 +271,14 @@ print_dhcp4_config (NMDhcpConfig *dhcp4, gpointer key, value; char **options_arr = NULL; int i = 0; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); tmpl = nmc_fields_dhcp4_config; tmpl_len = sizeof (nmc_fields_dhcp4_config); - nmc->out.print_fields.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_DHCP4_CONFIG_ALL, - tmpl, FALSE, NULL, NULL); + out.print_fields.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_DHCP4_CONFIG_ALL, + tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); options_arr = g_new (char *, g_hash_table_size (table) + 1); g_hash_table_iter_init (&table_iter, table); @@ -290,12 +289,10 @@ print_dhcp4_config (NMDhcpConfig *dhcp4, arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX); set_val_strc (arr, 0, group_prefix); set_val_arr (arr, 1, options_arr); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); - - nmc_empty_output_fields (&nmc->out); + print_data_prepare_width (out.output_data); + print_data (nmc_config, &out); return TRUE; } @@ -304,7 +301,7 @@ print_dhcp4_config (NMDhcpConfig *dhcp4, gboolean print_dhcp6_config (NMDhcpConfig *dhcp6, - NmCli *nmc, + const NmcConfig *nmc_config, const char *group_prefix, const char *one_field) { @@ -321,13 +318,14 @@ print_dhcp6_config (NMDhcpConfig *dhcp6, gpointer key, value; char **options_arr = NULL; int i = 0; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); tmpl = nmc_fields_dhcp6_config; tmpl_len = sizeof (nmc_fields_dhcp6_config); - nmc->out.print_fields.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_DHCP6_CONFIG_ALL, - tmpl, FALSE, NULL, NULL); + out.print_fields.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_DHCP6_CONFIG_ALL, + tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); options_arr = g_new (char *, g_hash_table_size (table) + 1); g_hash_table_iter_init (&table_iter, table); @@ -338,12 +336,10 @@ print_dhcp6_config (NMDhcpConfig *dhcp6, arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX); set_val_strc (arr, 0, group_prefix); set_val_arr (arr, 1, options_arr); - g_ptr_array_add (nmc->out.output_data, arr); - - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + g_ptr_array_add (out.output_data, arr); - nmc_empty_output_fields (&nmc->out); + print_data_prepare_width (out.output_data); + print_data (nmc_config, &out); return TRUE; } diff --git a/clients/cli/common.h b/clients/cli/common.h index f3b4e702db..ca4b030820 100644 --- a/clients/cli/common.h +++ b/clients/cli/common.h @@ -25,10 +25,10 @@ #include "nmcli.h" #include "nm-secret-agent-simple.h" -gboolean print_ip4_config (NMIPConfig *cfg4, NmCli *nmc, const char *group_prefix, const char *one_field); -gboolean print_ip6_config (NMIPConfig *cfg6, NmCli *nmc, const char *group_prefix, const char *one_field); -gboolean print_dhcp4_config (NMDhcpConfig *dhcp4, NmCli *nmc, const char *group_prefix, const char *one_field); -gboolean print_dhcp6_config (NMDhcpConfig *dhcp6, NmCli *nmc, const char *group_prefix, const char *one_field); +gboolean print_ip4_config (NMIPConfig *cfg4, const NmcConfig *nmc_config, const char *group_prefix, const char *one_field); +gboolean print_ip6_config (NMIPConfig *cfg6, const NmcConfig *nmc_config, const char *group_prefix, const char *one_field); +gboolean print_dhcp4_config (NMDhcpConfig *dhcp4, const NmcConfig *nmc_config, const char *group_prefix, const char *one_field); +gboolean print_dhcp6_config (NMDhcpConfig *dhcp6, const NmcConfig *nmc_config, const char *group_prefix, const char *one_field); const char * nmc_device_state_to_string (NMDeviceState state); const char * nmc_device_reason_to_string (NMDeviceStateReason reason); diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 9ab5c8395e..be52b75034 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -790,12 +790,16 @@ nmc_connection_profile_details (NMConnection *connection, NmCli *nmc, gboolean s g_assert (print_settings_array); /* Main header */ - nmc->out.print_fields.header_name = (char *) construct_header_name (base_hdr, nm_connection_get_id (connection)); - nmc->out.print_fields.indices = parse_output_fields (NMC_FIELDS_SETTINGS_NAMES_ALL, - nmc_fields_settings_names, FALSE, NULL, NULL); + { + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); - nmc_fields_settings_names[0].flags = NMC_OF_FLAG_MAIN_HEADER_ONLY; - print_required_fields (&nmc->nmc_config, &nmc->out.print_fields, nmc_fields_settings_names); + out.print_fields.header_name = (char *) construct_header_name (base_hdr, nm_connection_get_id (connection)); + out.print_fields.indices = parse_output_fields (NMC_FIELDS_SETTINGS_NAMES_ALL, + nmc_fields_settings_names, FALSE, NULL, NULL); + + nmc_fields_settings_names[0].flags = NMC_OF_FLAG_MAIN_HEADER_ONLY; + print_required_fields (&nmc->nmc_config, &out.print_fields, nmc_fields_settings_names); + } /* Loop through the required settings and print them. */ for (i = 0; i < print_settings_array->len; i++) { @@ -808,13 +812,10 @@ nmc_connection_profile_details (NMConnection *connection, NmCli *nmc, gboolean s was_output = FALSE; - nmc_empty_output_fields (&nmc->out); - setting = nm_connection_get_setting_by_name (connection, nmc_fields_settings_names[section_idx].name); if (setting) { setting_details (setting, nmc, prop_name, secrets); was_output = TRUE; - continue; } } @@ -916,7 +917,7 @@ nmc_active_connection_state_to_color (NMActiveConnectionState state, NmcTermColo } static void -fill_output_connection (NMConnection *connection, NmCli *nmc, gboolean active_only) +fill_output_connection (NMConnection *connection, NMClient *client, GPtrArray *output_data, gboolean active_only) { NMSettingConnection *s_con; guint64 timestamp; @@ -935,7 +936,7 @@ fill_output_connection (NMConnection *connection, NmCli *nmc, gboolean active_on s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); - ac = get_ac_for_connection (nm_client_get_active_connections (nmc->client), connection); + ac = get_ac_for_connection (nm_client_get_active_connections (client), connection); if (active_only && !ac) return; @@ -979,11 +980,11 @@ fill_output_connection (NMConnection *connection, NmCli *nmc, gboolean active_on set_val_strc (arr, 12, ac_path); set_val_strc (arr, 13, nm_setting_connection_get_slave_type (s_con)); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (output_data, arr); } static void -fill_output_connection_for_invisible (NMActiveConnection *ac, NmCli *nmc) +fill_output_connection_for_invisible (NMActiveConnection *ac, GPtrArray *output_data) { NmcOutputField *arr; const char *ac_path = NULL; @@ -1016,12 +1017,12 @@ fill_output_connection_for_invisible (NMActiveConnection *ac, NmCli *nmc) set_val_color_fmt_all (arr, NMC_TERM_FORMAT_DIM); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (output_data, arr); } static void fill_output_active_connection (NMActiveConnection *active, - NmCli *nmc, + GPtrArray *output_data, gboolean with_group, guint32 o_flags) { @@ -1088,7 +1089,7 @@ fill_output_active_connection (NMActiveConnection *active, set_val_strc (arr, 12-idx_start, master ? nm_object_get_path (NM_OBJECT (master)) : NULL); set_val_strc (arr, 13-idx_start, s_con ? nm_setting_connection_get_slave_type (s_con) : NULL); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (output_data, arr); g_string_free (dev_str, FALSE); } @@ -1216,12 +1217,16 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) g_assert (print_groups); /* Main header */ - nmc->out.print_fields.header_name = (char *) construct_header_name (base_hdr, nm_active_connection_get_uuid (acon)); - nmc->out.print_fields.indices = parse_output_fields (NMC_FIELDS_CON_ACTIVE_DETAILS_ALL, - nmc_fields_con_active_details_groups, FALSE, NULL, NULL); + { + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); + + out.print_fields.header_name = (char *) construct_header_name (base_hdr, nm_active_connection_get_uuid (acon)); + out.print_fields.indices = parse_output_fields (NMC_FIELDS_CON_ACTIVE_DETAILS_ALL, + nmc_fields_con_active_details_groups, FALSE, NULL, NULL); - nmc_fields_con_active_details_groups[0].flags = NMC_OF_FLAG_MAIN_HEADER_ONLY; - print_required_fields (&nmc->nmc_config, &nmc->out.print_fields, nmc_fields_con_active_details_groups); + nmc_fields_con_active_details_groups[0].flags = NMC_OF_FLAG_MAIN_HEADER_ONLY; + print_required_fields (&nmc->nmc_config, &out.print_fields, nmc_fields_con_active_details_groups); + } /* Loop through the groups and print them. */ for (i = 0; i < print_groups->len; i++) { @@ -1233,23 +1238,23 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) was_output = FALSE; - nmc_empty_output_fields (&nmc->out); - /* GENERAL */ if (strcasecmp (nmc_fields_con_active_details_groups[group_idx].name, nmc_fields_con_active_details_groups[0].name) == 0) { + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); + /* Add field names */ tmpl = nmc_fields_con_active_details_general; tmpl_len = sizeof (nmc_fields_con_active_details_general); - nmc->out.print_fields.indices = parse_output_fields (group_fld ? group_fld : NMC_FIELDS_CON_ACTIVE_DETAILS_GENERAL_ALL, - tmpl, FALSE, NULL, NULL); + out.print_fields.indices = parse_output_fields (group_fld ? group_fld : NMC_FIELDS_CON_ACTIVE_DETAILS_GENERAL_ALL, + tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); /* Fill in values */ - fill_output_active_connection (acon, nmc, TRUE, NMC_OF_FLAG_SECTION_PREFIX); + fill_output_active_connection (acon, out.output_data, TRUE, NMC_OF_FLAG_SECTION_PREFIX); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); was_output = TRUE; } @@ -1259,7 +1264,7 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) gboolean b1 = FALSE; NMIPConfig *cfg4 = nm_active_connection_get_ip4_config (acon); - b1 = print_ip4_config (cfg4, nmc, "IP4", group_fld); + b1 = print_ip4_config (cfg4, &nmc->nmc_config, "IP4", group_fld); was_output = was_output || b1; } @@ -1268,7 +1273,7 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) gboolean b1 = FALSE; NMDhcpConfig *dhcp4 = nm_active_connection_get_dhcp4_config (acon); - b1 = print_dhcp4_config (dhcp4, nmc, "DHCP4", group_fld); + b1 = print_dhcp4_config (dhcp4, &nmc->nmc_config, "DHCP4", group_fld); was_output = was_output || b1; } @@ -1277,7 +1282,7 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) gboolean b1 = FALSE; NMIPConfig *cfg6 = nm_active_connection_get_ip6_config (acon); - b1 = print_ip6_config (cfg6, nmc, "IP6", group_fld); + b1 = print_ip6_config (cfg6, &nmc->nmc_config, "IP6", group_fld); was_output = was_output || b1; } @@ -1286,7 +1291,7 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) gboolean b1 = FALSE; NMDhcpConfig *dhcp6 = nm_active_connection_get_dhcp6_config (acon); - b1 = print_dhcp6_config (dhcp6, nmc, "DHCP6", group_fld); + b1 = print_dhcp6_config (dhcp6, &nmc->nmc_config, "DHCP6", group_fld); was_output = was_output || b1; } @@ -1302,6 +1307,7 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) const char *username = NULL; char **vpn_data_array = NULL; guint32 items_num; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); con = NM_CONNECTION (nm_active_connection_get_connection (acon)); @@ -1310,10 +1316,10 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) tmpl = nmc_fields_con_active_details_vpn; tmpl_len = sizeof (nmc_fields_con_active_details_vpn); - nmc->out.print_fields.indices = parse_output_fields (group_fld ? group_fld : NMC_FIELDS_CON_ACTIVE_DETAILS_VPN_ALL, - tmpl, FALSE, NULL, NULL); + out.print_fields.indices = parse_output_fields (group_fld ? group_fld : NMC_FIELDS_CON_ACTIVE_DETAILS_VPN_ALL, + tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); s_vpn = nm_connection_get_setting_vpn (con); if (s_vpn) { @@ -1346,10 +1352,10 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) set_val_str (arr, 4, banner_str); set_val_str (arr, 5, vpn_state_str); set_val_arr (arr, 6, vpn_data_array); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); was_output = TRUE; } } @@ -1794,6 +1800,7 @@ do_connections_show (NmCli *nmc, int argc, char **argv) char *fields_common = NMC_FIELDS_CON_SHOW_COMMON; NmcOutputField *tmpl, *arr; size_t tmpl_len; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); if (nmc->complete) goto finish; @@ -1807,32 +1814,32 @@ do_connections_show (NmCli *nmc, int argc, char **argv) tmpl = nmc_fields_con_show; tmpl_len = sizeof (nmc_fields_con_show); - nmc->out.print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &err); + out.print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &err); if (err) goto finish; /* Add headers */ - nmc->out.print_fields.header_name = active_only ? _("NetworkManager active profiles") : + out.print_fields.header_name = active_only ? _("NetworkManager active profiles") : _("NetworkManager connection profiles"); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); /* There might be active connections not present in connection list * (e.g. private connections of a different user). Show them as well. */ invisibles = get_invisible_active_connections (nmc); for (i = 0; i < invisibles->len; i++) - fill_output_connection_for_invisible (invisibles->pdata[i], nmc); + fill_output_connection_for_invisible (invisibles->pdata[i], out.output_data); g_ptr_array_free (invisibles, TRUE); /* Sort the connections and fill the output data */ connections = nm_client_get_connections (nmc->client); sorted_cons = sort_connections (connections, nmc, order); for (i = 0; i < sorted_cons->len; i++) - fill_output_connection (sorted_cons->pdata[i], nmc, active_only); + fill_output_connection (sorted_cons->pdata[i], nmc->client, out.output_data, active_only); g_ptr_array_free (sorted_cons, TRUE); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); } else { gboolean new_line = FALSE; gboolean without_fields = (nmc->required_fields == NULL); @@ -6020,8 +6027,6 @@ editor_show_connection (NMConnection *connection, NmCli *nmc) nmc->nmc_config_mutable.multiline_output = TRUE; nmc->nmc_config_mutable.escape_values = 0; - nmc_empty_output_fields (&nmc->out); - nmc_connection_profile_details (connection, nmc, nmc->editor_show_secrets); } @@ -6035,8 +6040,6 @@ editor_show_setting (NMSetting *setting, NmCli *nmc) nmc->nmc_config_mutable.multiline_output = TRUE; nmc->nmc_config_mutable.escape_values = 0; - nmc_empty_output_fields (&nmc->out); - setting_details (setting, nmc, NULL, nmc->editor_show_secrets); } diff --git a/clients/cli/devices.c b/clients/cli/devices.c index 050abd1171..69c913a9d5 100644 --- a/clients/cli/devices.c +++ b/clients/cli/devices.c @@ -766,6 +766,7 @@ typedef struct { guint32 output_flags; const char* active_bssid; const char* device; + GPtrArray *output_data; } APInfo; static void @@ -881,7 +882,7 @@ fill_output_access_point (gpointer data, gpointer user_data) if (active) arr[15].color = NMC_TERM_COLOR_GREEN; - g_ptr_array_add (info->nmc->out.output_data, arr); + g_ptr_array_add (info->output_data, arr); g_string_free (security_str, FALSE); } @@ -951,6 +952,7 @@ print_bond_bridge_info (NMDevice *device, int idx; NmcOutputField *tmpl, *arr; size_t tmpl_len; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); if (NM_IS_DEVICE_BOND (device)) slaves = nm_device_bond_get_slaves (NM_DEVICE_BOND (device)); @@ -974,21 +976,20 @@ print_bond_bridge_info (NMDevice *device, tmpl = nmc_fields_dev_show_master_prop; tmpl_len = sizeof (nmc_fields_dev_show_master_prop); - nmc->out.print_fields.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_DEV_SHOW_MASTER_PROP_ALL, - tmpl, FALSE, NULL, NULL); + out.print_fields.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_DEV_SHOW_MASTER_PROP_ALL, + tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX); set_val_strc (arr, 0, group_prefix); /* i.e. BOND, TEAM, BRIDGE */ set_val_str (arr, 1, slaves_str->str); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); g_string_free (slaves_str, FALSE); - nmc_empty_output_fields (&nmc->out); return TRUE; } @@ -1023,6 +1024,7 @@ print_team_info (NMDevice *device, int idx; NmcOutputField *tmpl, *arr; size_t tmpl_len; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); if (NM_IS_DEVICE_TEAM (device)) slaves = nm_device_team_get_slaves (NM_DEVICE_TEAM (device)); @@ -1044,22 +1046,22 @@ print_team_info (NMDevice *device, tmpl = nmc_fields_dev_show_team_prop; tmpl_len = sizeof (nmc_fields_dev_show_team_prop); - nmc->out.print_fields.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_DEV_SHOW_TEAM_PROP_ALL, + out.print_fields.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_DEV_SHOW_TEAM_PROP_ALL, tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX); set_val_strc (arr, 0, group_prefix); /* TEAM */ set_val_str (arr, 1, slaves_str->str); set_val_str (arr, 2, sanitize_team_config (nm_device_team_get_config (NM_DEVICE_TEAM (device)))); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); g_string_free (slaves_str, FALSE); - nmc_empty_output_fields (&nmc->out); + nmc_empty_output_fields (&out); return TRUE; } @@ -1068,7 +1070,6 @@ static gboolean show_device_info (NMDevice *device, NmCli *nmc) { GError *error = NULL; - APInfo *info; const char *hwaddr = NULL; NMDeviceState state = NM_DEVICE_STATE_UNKNOWN; NMDeviceStateReason reason = NM_DEVICE_STATE_REASON_NONE; @@ -1104,15 +1105,19 @@ show_device_info (NMDevice *device, NmCli *nmc) return FALSE; } - /* Main header (pretty only) */ - nmc->out.print_fields.header_name = (char *) construct_header_name (base_hdr, nm_device_get_iface (device)); + { + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); - /* Lazy way to retrieve sorted array from 0 to the number of dev fields */ - nmc->out.print_fields.indices = parse_output_fields (NMC_FIELDS_DEV_SHOW_GENERAL_ALL, - nmc_fields_dev_show_general, FALSE, NULL, NULL); + /* Main header (pretty only) */ + out.print_fields.header_name = (char *) construct_header_name (base_hdr, nm_device_get_iface (device)); - nmc_fields_dev_show_general[0].flags = NMC_OF_FLAG_MAIN_HEADER_ONLY; - print_required_fields (&nmc->nmc_config, &nmc->out.print_fields, nmc_fields_dev_show_general); + /* Lazy way to retrieve sorted array from 0 to the number of dev fields */ + out.print_fields.indices = parse_output_fields (NMC_FIELDS_DEV_SHOW_GENERAL_ALL, + nmc_fields_dev_show_general, FALSE, NULL, NULL); + + nmc_fields_dev_show_general[0].flags = NMC_OF_FLAG_MAIN_HEADER_ONLY; + print_required_fields (&nmc->nmc_config, &out.print_fields, nmc_fields_dev_show_general); + } /* Loop through the required sections and print them. */ for (k = 0; k < sections_array->len; k++) { @@ -1124,19 +1129,19 @@ show_device_info (NMDevice *device, NmCli *nmc) was_output = FALSE; - nmc_empty_output_fields (&nmc->out); - state = nm_device_get_state (device); reason = nm_device_get_state_reason (device); /* section GENERAL */ if (!strcasecmp (nmc_fields_dev_show_sections[section_idx].name, nmc_fields_dev_show_sections[0].name)) { + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); + tmpl = nmc_fields_dev_show_general; tmpl_len = sizeof (nmc_fields_dev_show_general); - nmc->out.print_fields.indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_SHOW_GENERAL_ALL, - tmpl, FALSE, NULL, NULL); + out.print_fields.indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_SHOW_GENERAL_ALL, + tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); state_str = g_strdup_printf ("%d (%s)", state, nmc_device_state_to_string (state)); reason_str = g_strdup_printf ("%d (%s)", reason, nmc_device_reason_to_string (reason)); @@ -1170,21 +1175,23 @@ show_device_info (NMDevice *device, NmCli *nmc) set_val_strc (arr, 22, acon ? nm_active_connection_get_uuid (acon) : NULL); set_val_strc (arr, 23, acon ? nm_object_get_path (NM_OBJECT (acon)) : NULL); set_val_strc (arr, 24, nmc_device_metered_to_string (nm_device_get_metered (device))); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); was_output = TRUE; } /* section CAPABILITIES */ if (!strcasecmp (nmc_fields_dev_show_sections[section_idx].name, nmc_fields_dev_show_sections[1].name)) { + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); + tmpl = nmc_fields_dev_show_cap; tmpl_len = sizeof (nmc_fields_dev_show_cap); - nmc->out.print_fields.indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_SHOW_CAP_ALL, - tmpl, FALSE, NULL, NULL); + out.print_fields.indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_SHOW_CAP_ALL, + tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); caps = nm_device_get_capabilities (device); speed = 0; @@ -1203,10 +1210,10 @@ show_device_info (NMDevice *device, NmCli *nmc) set_val_strc (arr, 1, (caps & NM_DEVICE_CAP_CARRIER_DETECT) ? _("yes") : _("no")); set_val_str (arr, 2, speed_str); set_val_strc (arr, 3, (caps & NM_DEVICE_CAP_IS_SOFTWARE) ? _("yes") : _("no")); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); was_output = TRUE; } @@ -1219,14 +1226,16 @@ show_device_info (NMDevice *device, NmCli *nmc) /* section WIFI-PROPERTIES */ if (!strcasecmp (nmc_fields_dev_show_sections[section_idx].name, nmc_fields_dev_show_sections[2].name)) { + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); + wcaps = nm_device_wifi_get_capabilities (NM_DEVICE_WIFI (device)); tmpl = nmc_fields_dev_show_wifi_prop; tmpl_len = sizeof (nmc_fields_dev_show_wifi_prop); - nmc->out.print_fields.indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_SHOW_WIFI_PROP_ALL, - tmpl, FALSE, NULL, NULL); + out.print_fields.indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_SHOW_WIFI_PROP_ALL, + tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX); set_val_strc (arr, 0, nmc_fields_dev_show_sections[2].name); /* "WIFI-PROPERTIES" */ @@ -1242,15 +1251,17 @@ show_device_info (NMDevice *device, NmCli *nmc) ((wcaps & NM_WIFI_DEVICE_CAP_FREQ_2GHZ) ? _("yes") : _("no"))); set_val_strc (arr, 9, !(wcaps & NM_WIFI_DEVICE_CAP_FREQ_VALID) ? _("unknown") : ((wcaps & NM_WIFI_DEVICE_CAP_FREQ_5GHZ) ? _("yes") : _("no"))); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); was_output = TRUE; } /* section AP */ if (!strcasecmp (nmc_fields_dev_show_sections[section_idx].name, nmc_fields_dev_show_sections[3].name)) { + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); + if (state == NM_DEVICE_STATE_ACTIVATED) { active_ap = nm_device_wifi_get_active_access_point (NM_DEVICE_WIFI (device)); active_bssid = active_ap ? nm_access_point_get_bssid (active_ap) : NULL; @@ -1258,44 +1269,51 @@ show_device_info (NMDevice *device, NmCli *nmc) tmpl = nmc_fields_dev_wifi_list; tmpl_len = sizeof (nmc_fields_dev_wifi_list); - nmc->out.print_fields.indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_WIFI_LIST_FOR_DEV_LIST, - tmpl, FALSE, NULL, NULL); + out.print_fields.indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_WIFI_LIST_FOR_DEV_LIST, + tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); + + { + APInfo info = { + .nmc = nmc, + .index = 1, + .output_flags = NMC_OF_FLAG_SECTION_PREFIX, + .active_bssid = active_bssid, + .device = nm_device_get_iface (device), + .output_data = out.output_data, + }; + + aps = sort_access_points (nm_device_wifi_get_access_points (NM_DEVICE_WIFI (device))); + g_ptr_array_foreach (aps, fill_output_access_point, &info); + g_ptr_array_free (aps, FALSE); + } - info = g_malloc0 (sizeof (APInfo)); - info->nmc = nmc; - info->index = 1; - info->output_flags = NMC_OF_FLAG_SECTION_PREFIX; - info->active_bssid = active_bssid; - info->device = nm_device_get_iface (device); - aps = sort_access_points (nm_device_wifi_get_access_points (NM_DEVICE_WIFI (device))); - g_ptr_array_foreach ((GPtrArray *) aps, fill_output_access_point, (gpointer) info); - g_ptr_array_free (aps, FALSE); - g_free (info); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); was_output = TRUE; } } else if (NM_IS_DEVICE_ETHERNET (device)) { /* WIRED-PROPERTIES */ if (!strcasecmp (nmc_fields_dev_show_sections[section_idx].name, nmc_fields_dev_show_sections[4].name)) { + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); + tmpl = nmc_fields_dev_show_wired_prop; tmpl_len = sizeof (nmc_fields_dev_show_wired_prop); - nmc->out.print_fields.indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_SHOW_WIRED_PROP_ALL, - tmpl, FALSE, NULL, NULL); + out.print_fields.indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_SHOW_WIRED_PROP_ALL, + tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX); set_val_strc (arr, 0, nmc_fields_dev_show_sections[4].name); /* "WIRED-PROPERTIES" */ set_val_strc (arr, 1, (nm_device_ethernet_get_carrier (NM_DEVICE_ETHERNET (device))) ? _("on") : _("off")); set_val_arrc (arr, 2, ((const char **) nm_device_ethernet_get_s390_subchannels (NM_DEVICE_ETHERNET (device)))); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); was_output = TRUE; } } @@ -1308,19 +1326,19 @@ show_device_info (NMDevice *device, NmCli *nmc) /* IP4 */ if (cfg4 && !strcasecmp (nmc_fields_dev_show_sections[section_idx].name, nmc_fields_dev_show_sections[7].name)) - was_output = print_ip4_config (cfg4, nmc, nmc_fields_dev_show_sections[7].name, section_fld); + was_output = print_ip4_config (cfg4, &nmc->nmc_config, nmc_fields_dev_show_sections[7].name, section_fld); /* DHCP4 */ if (dhcp4 && !strcasecmp (nmc_fields_dev_show_sections[section_idx].name, nmc_fields_dev_show_sections[8].name)) - was_output = print_dhcp4_config (dhcp4, nmc, nmc_fields_dev_show_sections[8].name, section_fld); + was_output = print_dhcp4_config (dhcp4, &nmc->nmc_config, nmc_fields_dev_show_sections[8].name, section_fld); /* IP6 */ if (cfg6 && !strcasecmp (nmc_fields_dev_show_sections[section_idx].name, nmc_fields_dev_show_sections[9].name)) - was_output = print_ip6_config (cfg6, nmc, nmc_fields_dev_show_sections[9].name, section_fld); + was_output = print_ip6_config (cfg6, &nmc->nmc_config, nmc_fields_dev_show_sections[9].name, section_fld); /* DHCP6 */ if (dhcp6 && !strcasecmp (nmc_fields_dev_show_sections[section_idx].name, nmc_fields_dev_show_sections[10].name)) - was_output = print_dhcp6_config (dhcp6, nmc, nmc_fields_dev_show_sections[10].name, section_fld); + was_output = print_dhcp6_config (dhcp6, &nmc->nmc_config, nmc_fields_dev_show_sections[10].name, section_fld); /* Bond specific information */ if (NM_IS_DEVICE_BOND (device)) { @@ -1345,22 +1363,23 @@ show_device_info (NMDevice *device, NmCli *nmc) if (!strcasecmp (nmc_fields_dev_show_sections[section_idx].name, nmc_fields_dev_show_sections[14].name)) { char * vlan_id_str = g_strdup_printf ("%u", nm_device_vlan_get_vlan_id (NM_DEVICE_VLAN (device))); NMDevice *parent = nm_device_vlan_get_parent (NM_DEVICE_VLAN (device)); + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); tmpl = nmc_fields_dev_show_vlan_prop; tmpl_len = sizeof (nmc_fields_dev_show_vlan_prop); - nmc->out.print_fields.indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_SHOW_VLAN_PROP_ALL, - tmpl, FALSE, NULL, NULL); + out.print_fields.indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_SHOW_VLAN_PROP_ALL, + tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX); set_val_strc (arr, 0, nmc_fields_dev_show_sections[14].name); /* "VLAN" */ set_val_strc (arr, 1, parent ? nm_device_get_iface (parent) : NULL); set_val_str (arr, 2, vlan_id_str); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); was_output = TRUE; } @@ -1368,20 +1387,22 @@ show_device_info (NMDevice *device, NmCli *nmc) if (NM_IS_DEVICE_BT (device)) { if (!strcasecmp (nmc_fields_dev_show_sections[section_idx].name, nmc_fields_dev_show_sections[15].name)) { + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); + tmpl = nmc_fields_dev_show_bluetooth; tmpl_len = sizeof (nmc_fields_dev_show_bluetooth); - nmc->out.print_fields.indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_SHOW_BLUETOOTH_ALL, - tmpl, FALSE, NULL, NULL); + out.print_fields.indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_SHOW_BLUETOOTH_ALL, + tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX); set_val_strc (arr, 0, nmc_fields_dev_show_sections[15].name); /* "BLUETOOTH" */ set_val_str (arr, 1, bluetooth_caps_to_string (nm_device_bt_get_capabilities (NM_DEVICE_BT (device)))); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); was_output = TRUE; } } @@ -1392,13 +1413,14 @@ show_device_info (NMDevice *device, NmCli *nmc) GString *ac_paths_str; char **ac_arr = NULL; int i; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); tmpl = nmc_fields_dev_show_connections; tmpl_len = sizeof (nmc_fields_dev_show_connections); - nmc->out.print_fields.indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_SHOW_CONNECTIONS_ALL, - tmpl, FALSE, NULL, NULL); + out.print_fields.indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_SHOW_CONNECTIONS_ALL, + tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); /* available-connections */ avail_cons = nm_device_get_available_connections (device); @@ -1428,10 +1450,10 @@ show_device_info (NMDevice *device, NmCli *nmc) set_val_strc (arr, 0, nmc_fields_dev_show_sections[16].name); /* "CONNECTIONS" */ set_val_str (arr, 1, ac_paths_str->str); set_val_arr (arr, 2, (ac_arr)); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); g_string_free (ac_paths_str, FALSE); was_output = TRUE; @@ -1463,7 +1485,7 @@ nmc_device_state_to_color (NMDeviceState state, NmcTermColor *color, NmcTermForm } static void -fill_output_device_status (NMDevice *device, NmCli *nmc) +fill_output_device_status (NMDevice *device, GPtrArray *output_data) { NMActiveConnection *ac; NMDeviceState state; @@ -1489,7 +1511,7 @@ fill_output_device_status (NMDevice *device, NmCli *nmc) set_val_strc (arr, 5, ac ? nm_active_connection_get_uuid (ac) : NULL); set_val_strc (arr, 6, ac ? nm_object_get_path (NM_OBJECT (ac)) : NULL); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (output_data, arr); } static NMCResultCode @@ -1503,6 +1525,7 @@ do_devices_status (NmCli *nmc, int argc, char **argv) char *fields_common = NMC_FIELDS_DEV_STATUS_COMMON; NmcOutputField *tmpl, *arr; size_t tmpl_len; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); /* Nothing to complete */ if (nmc->complete) @@ -1522,7 +1545,7 @@ do_devices_status (NmCli *nmc, int argc, char **argv) tmpl = nmc_fields_dev_status; tmpl_len = sizeof (nmc_fields_dev_status); - nmc->out.print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error); + out.print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error); if (error) { g_string_printf (nmc->return_text, _("Error: 'device status': %s"), error->message); @@ -1531,16 +1554,16 @@ do_devices_status (NmCli *nmc, int argc, char **argv) } /* Add headers */ - nmc->out.print_fields.header_name = _("Status of devices"); + out.print_fields.header_name = _("Status of devices"); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); devices = nmc_get_devices_sorted (nmc->client); for (i = 0; devices[i]; i++) - fill_output_device_status (devices[i], nmc); + fill_output_device_status (devices[i], out.output_data); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); g_free (devices); @@ -1582,7 +1605,6 @@ do_device_show (NmCli *nmc, int argc, char **argv) /* Show details for all devices */ for (i = 0; devices[i]; i++) { - nmc_empty_output_fields (&nmc->out); if (!show_device_info (devices[i], nmc)) break; if (devices[i + 1]) @@ -2478,12 +2500,11 @@ do_devices_monitor (NmCli *nmc, int argc, char **argv) } static void -show_access_point_info (NMDevice *device, NmCli *nmc) +show_access_point_info (NMDevice *device, NmCli *nmc, NmcOutputData *out) { NMAccessPoint *active_ap = NULL; const char *active_bssid = NULL; GPtrArray *aps; - APInfo *info; NmcOutputField *arr; if (nm_device_get_state (device) == NM_DEVICE_STATE_ACTIVATED) { @@ -2493,22 +2514,25 @@ show_access_point_info (NMDevice *device, NmCli *nmc) arr = nmc_dup_fields_array (nmc_fields_dev_wifi_list, sizeof (nmc_fields_dev_wifi_list), NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out->output_data, arr); - info = g_malloc0 (sizeof (APInfo)); - info->nmc = nmc; - info->index = 1; - info->output_flags = 0; - info->active_bssid = active_bssid; - info->device = nm_device_get_iface (device); - aps = sort_access_points (nm_device_wifi_get_access_points (NM_DEVICE_WIFI (device))); - g_ptr_array_foreach ((GPtrArray *) aps, fill_output_access_point, (gpointer) info); - g_ptr_array_free (aps, FALSE); - - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); - nmc_empty_output_fields (&nmc->out); - g_free (info); + { + APInfo info = { + .nmc = nmc, + .index = 1, + .output_flags = 0, + .active_bssid = active_bssid, + .device = nm_device_get_iface (device), + .output_data = out->output_data, + }; + + aps = sort_access_points (nm_device_wifi_get_access_points (NM_DEVICE_WIFI (device))); + g_ptr_array_foreach (aps, fill_output_access_point, &info); + g_ptr_array_free (aps, FALSE); + } + + print_data_prepare_width (out->output_data); + print_data (&nmc->nmc_config, out); } /* @@ -2641,6 +2665,7 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv) NmcOutputField *tmpl, *arr; size_t tmpl_len; const char *base_hdr = _("Wi-Fi scan list"); + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); devices = nmc_get_devices_sorted (nmc->client); @@ -2684,7 +2709,7 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv) tmpl = nmc_fields_dev_wifi_list; tmpl_len = sizeof (nmc_fields_dev_wifi_list); - nmc->out.print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error); + out.print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error); if (error) { g_string_printf (nmc->return_text, _("Error: 'device wifi': %s"), error->message); @@ -2702,7 +2727,7 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv) return NMC_RESULT_ERROR_NOT_FOUND; } /* Main header name */ - nmc->out.print_fields.header_name = (char *) construct_header_name (base_hdr, ifname); + out.print_fields.header_name = (char *) construct_header_name (base_hdr, ifname); if (NM_IS_DEVICE_WIFI (device)) { if (bssid_user) { @@ -2725,7 +2750,7 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv) } /* Add headers (field names) */ arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); info = g_malloc0 (sizeof (APInfo)); info->nmc = nmc; @@ -2736,11 +2761,11 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv) fill_output_access_point (ap, info); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); g_free (info); } else { - show_access_point_info (device, nmc); + show_access_point_info (device, nmc, &out); } } else { if ( nm_device_get_device_type (device) == NM_DEVICE_TYPE_GENERIC @@ -2768,11 +2793,11 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv) continue; /* Main header name */ - nmc->out.print_fields.header_name = (char *) construct_header_name (base_hdr, nm_device_get_iface (dev)); - nmc->out.print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, NULL); + out.print_fields.header_name = (char *) construct_header_name (base_hdr, nm_device_get_iface (dev)); + out.print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); aps = nm_device_wifi_get_access_points (NM_DEVICE_WIFI (dev)); for (j = 0; j < aps->len; j++) { @@ -2797,9 +2822,9 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv) } if (empty_line) g_print ("\n"); /* Empty line between devices' APs */ - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); - nmc_empty_output_fields (&nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); + nmc_empty_output_fields (&out); empty_line = TRUE; } if (!ap) { @@ -2810,16 +2835,17 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv) } else { for (i = 0; devices[i]; i++) { NMDevice *dev = devices[i]; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out2); /* Main header name */ - nmc->out.print_fields.header_name = (char *) construct_header_name (base_hdr, + out2.print_fields.header_name = (char *) construct_header_name (base_hdr, nm_device_get_iface (dev)); - nmc->out.print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, NULL); + out2.print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, NULL); if (NM_IS_DEVICE_WIFI (dev)) { if (empty_line) g_print ("\n"); /* Empty line between devices' APs */ - show_access_point_info (dev, nmc); + show_access_point_info (dev, nmc, &out2); empty_line = TRUE; } } @@ -3676,6 +3702,7 @@ show_device_lldp_list (NMDevice *device, NmCli *nmc, char *fields_str, int *coun size_t tmpl_len; const char *str; int i; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); neighbors = nm_device_get_lldp_neighbors (device); @@ -3686,11 +3713,11 @@ show_device_lldp_list (NMDevice *device, NmCli *nmc, char *fields_str, int *coun tmpl_len = sizeof (nmc_fields_dev_lldp_list); /* Main header name */ - nmc->out.print_fields.header_name = (char *) construct_header_name (_("Device LLDP neighbors"), - nm_device_get_iface (device)); - nmc->out.print_fields.indices = parse_output_fields (fields_str, nmc_fields_dev_lldp_list, FALSE, NULL, NULL); + out.print_fields.header_name = (char *) construct_header_name (_("Device LLDP neighbors"), + nm_device_get_iface (device)); + out.print_fields.indices = parse_output_fields (fields_str, nmc_fields_dev_lldp_list, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); for (i = 0; i < neighbors->len; i++) { NMLldpNeighbor *neighbor = neighbors->pdata[i]; @@ -3743,12 +3770,11 @@ show_device_lldp_list (NMDevice *device, NmCli *nmc, char *fields_str, int *coun if (nm_lldp_neighbor_get_attr_uint_value (neighbor, NM_LLDP_ATTR_PORT_ID_TYPE, &value)) set_val_strc (arr, 15, g_strdup_printf ("%u", value)); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); } - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); - nmc_empty_output_fields (&nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); return neighbors->len; } @@ -3760,6 +3786,7 @@ do_device_lldp_list (NmCli *nmc, int argc, char **argv) gs_free_error GError *error = NULL; char *fields_str; int counter = 0; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); while (argc > 0) { if (argc == 1 && nmc->complete) @@ -3793,7 +3820,7 @@ do_device_lldp_list (NmCli *nmc, int argc, char **argv) else fields_str = nmc->required_fields; - nmc->out.print_fields.indices = parse_output_fields (fields_str, nmc_fields_dev_lldp_list, FALSE, NULL, &error); + out.print_fields.indices = parse_output_fields (fields_str, nmc_fields_dev_lldp_list, FALSE, NULL, &error); if (error) { g_string_printf (nmc->return_text, _("Error: 'device lldp list': %s"), error->message); @@ -3804,16 +3831,13 @@ do_device_lldp_list (NmCli *nmc, int argc, char **argv) return nmc->return_value; if (device) { - nmc_empty_output_fields (&nmc->out); show_device_lldp_list (device, nmc, fields_str, &counter); } else { NMDevice **devices = nmc_get_devices_sorted (nmc->client); int i; - for (i = 0; devices[i]; i++) { - nmc_empty_output_fields (&nmc->out); + for (i = 0; devices[i]; i++) show_device_lldp_list (devices[i], nmc, fields_str, &counter); - } g_free (devices); } diff --git a/clients/cli/general.c b/clients/cli/general.c index 490886831e..5bffa92707 100644 --- a/clients/cli/general.c +++ b/clients/cli/general.c @@ -325,6 +325,7 @@ show_nm_status (NmCli *nmc, const char *pretty_header_name, const char *print_fl const char *fields_common = print_flds ? print_flds : NMC_FIELDS_NM_STATUS_COMMON; NmcOutputField *tmpl, *arr; size_t tmpl_len; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0) fields_str = fields_common; @@ -335,7 +336,7 @@ show_nm_status (NmCli *nmc, const char *pretty_header_name, const char *print_fl tmpl = nmc_fields_nm_status; tmpl_len = sizeof (nmc_fields_nm_status); - nmc->out.print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error); + out.print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error); if (error) { g_string_printf (nmc->return_text, _("Error: only these fields are allowed: %s"), fields_all); @@ -353,9 +354,9 @@ show_nm_status (NmCli *nmc, const char *pretty_header_name, const char *print_fl wwan_hw_enabled = nm_client_wwan_hardware_get_enabled (nmc->client); wwan_enabled = nm_client_wwan_get_enabled (nmc->client); - nmc->out.print_fields.header_name = pretty_header_name ? (char *) pretty_header_name : _("NetworkManager status"); + out.print_fields.header_name = pretty_header_name ? (char *) pretty_header_name : _("NetworkManager status"); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); arr = nmc_dup_fields_array (tmpl, tmpl_len, 0); set_val_strc (arr, 0, _("running")); @@ -379,10 +380,10 @@ show_nm_status (NmCli *nmc, const char *pretty_header_name, const char *print_fl arr[8].color = wwan_hw_enabled ? NMC_TERM_COLOR_GREEN : NMC_TERM_COLOR_RED; arr[9].color = wwan_enabled ? NMC_TERM_COLOR_GREEN : NMC_TERM_COLOR_RED; - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); return TRUE; } @@ -473,6 +474,7 @@ print_permissions (void *user_data) const char *fields_common = NMC_FIELDS_NM_PERMISSIONS_COMMON; NmcOutputField *tmpl, *arr; size_t tmpl_len; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0) fields_str = fields_common; @@ -483,7 +485,7 @@ print_permissions (void *user_data) tmpl = nmc_fields_nm_permissions; tmpl_len = sizeof (nmc_fields_nm_permissions); - nmc->out.print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error); + out.print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error); if (error) { g_string_printf (nmc->return_text, _("Error: 'general permissions': %s"), error->message); @@ -492,9 +494,9 @@ print_permissions (void *user_data) return FALSE; } - nmc->out.print_fields.header_name = _("NetworkManager permissions"); + out.print_fields.header_name = _("NetworkManager permissions"); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); for (perm = NM_CLIENT_PERMISSION_NONE + 1; perm <= NM_CLIENT_PERMISSION_LAST; perm++) { @@ -503,10 +505,10 @@ print_permissions (void *user_data) arr = nmc_dup_fields_array (tmpl, tmpl_len, 0); set_val_strc (arr, 0, permission_to_string (perm)); set_val_strc (arr, 1, permission_result_to_string (perm_result)); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); } - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); quit (); return G_SOURCE_REMOVE; @@ -583,6 +585,7 @@ show_general_logging (NmCli *nmc) const char *fields_common = NMC_FIELDS_NM_LOGGING_COMMON; NmcOutputField *tmpl, *arr; size_t tmpl_len; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0) fields_str = fields_common; @@ -593,7 +596,7 @@ show_general_logging (NmCli *nmc) tmpl = nmc_fields_nm_logging; tmpl_len = sizeof (nmc_fields_nm_logging); - nmc->out.print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error); + out.print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error); if (error) { g_string_printf (nmc->return_text, _("Error: 'general logging': %s"), error->message); @@ -610,17 +613,17 @@ show_general_logging (NmCli *nmc) return FALSE; } - nmc->out.print_fields.header_name = _("NetworkManager logging"); + out.print_fields.header_name = _("NetworkManager logging"); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); arr = nmc_dup_fields_array (tmpl, tmpl_len, 0); set_val_str (arr, 0, level); set_val_str (arr, 1, domains); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); return TRUE; } diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index ab4c0435a6..2553596459 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -540,8 +540,6 @@ nmc_init (NmCli *nmc) nmc->mode_specified = FALSE; nmc->nmc_config_mutable.escape_values = TRUE; nmc->required_fields = NULL; - nmc->out.output_data = g_ptr_array_new_full (20, g_free); - memset (&nmc->out.print_fields, '\0', sizeof (NmcPrintFields)); nmc->ask = FALSE; nmc->complete = FALSE; nmc->show_secrets = FALSE; @@ -569,8 +567,6 @@ nmc_cleanup (NmCli *nmc) g_hash_table_destroy (nmc->pwds_hash); g_free (nmc->required_fields); - nmc_empty_output_fields (&nmc->out); - g_ptr_array_unref (nmc->out.output_data); nmc_polkit_agent_fini (nmc); } diff --git a/clients/cli/nmcli.h b/clients/cli/nmcli.h index b78d7ff265..1b9c3e72e1 100644 --- a/clients/cli/nmcli.h +++ b/clients/cli/nmcli.h @@ -172,7 +172,6 @@ typedef struct _NmCli { NmcConfig nmc_config_mutable; }; char *required_fields; /* Required fields in output: '--fields' option */ - NmcOutputData out; gboolean ask; /* Ask for missing parameters: option '--ask' */ gboolean complete; /* Autocomplete the command line */ gboolean show_secrets; /* Whether to display secrets (both input and output): option '--show-secrets' */ @@ -193,4 +192,11 @@ void nmc_clear_sigint (void); void nmc_set_sigquit_internal (void); void nmc_exit (void); +void nmc_empty_output_fields (NmcOutputData *output_data); + +#define NMC_OUTPUT_DATA_DEFINE_SCOPED(out) \ + nm_auto (nmc_empty_output_fields) NmcOutputData out = { \ + .output_data = g_ptr_array_new_full (20, g_free), \ + } + #endif /* NMC_NMCLI_H */ diff --git a/clients/cli/settings.c b/clients/cli/settings.c index b6050efd7c..d38787e89c 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -880,6 +880,7 @@ setting_details (NMSetting *setting, NmCli *nmc, const char *one_prop, gboolean size_t tmpl_len; gs_free char *s_all = NULL; NMMetaAccessorGetType type = NM_META_ACCESSOR_GET_TYPE_PRETTY; + NMC_OUTPUT_DATA_DEFINE_SCOPED (out); g_return_val_if_fail (NM_IS_SETTING (setting), FALSE); @@ -897,10 +898,10 @@ setting_details (NMSetting *setting, NmCli *nmc, const char *one_prop, gboolean tmpl_len = sizeof (NmcOutputField) * (setting_info->properties_num + 1); tmpl = g_memdup (_get_nmc_output_fields (setting_info), tmpl_len); - nmc->out.print_fields.indices = parse_output_fields (one_prop ?: (s_all = _all_properties (setting_info)), + out.print_fields.indices = parse_output_fields (one_prop ?: (s_all = _all_properties (setting_info)), tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX); for (i = 0; i < setting_info->properties_num; i++) { @@ -916,10 +917,10 @@ setting_details (NMSetting *setting, NmCli *nmc, const char *one_prop, gboolean set_val_str (arr, i, g_strdup (_(NM_META_TEXT_HIDDEN))); } - g_ptr_array_add (nmc->out.output_data, arr); + g_ptr_array_add (out.output_data, arr); - print_data_prepare_width (nmc->out.output_data); - print_data (&nmc->nmc_config, &nmc->out); + print_data_prepare_width (out.output_data); + print_data (&nmc->nmc_config, &out); return TRUE; } |