Commit aae721d0 authored by Thomas Haller's avatar Thomas Haller

cli: pass arguments for print_data separately of NmcOutputData

Don't pass on large structs of input arguments. It only convolutes
which arguments are passed, and where they come from.
parent f973f084
......@@ -103,7 +103,7 @@ print_ip4_config (NMIPConfig *cfg4,
tmpl = nmc_fields_ip4_config;
tmpl_len = sizeof (nmc_fields_ip4_config);
out.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_IP4_CONFIG_ALL,
out_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 (out.output_data, arr);
......@@ -164,7 +164,7 @@ print_ip4_config (NMIPConfig *cfg4,
g_ptr_array_add (out.output_data, arr);
print_data_prepare_width (out.output_data);
print_data (nmc_config, &out);
print_data (nmc_config, out_indices, NULL, 0, &out);
return TRUE;
}
......@@ -190,7 +190,7 @@ print_ip6_config (NMIPConfig *cfg6,
tmpl = nmc_fields_ip6_config;
tmpl_len = sizeof (nmc_fields_ip6_config);
out.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_IP6_CONFIG_ALL,
out_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 (out.output_data, arr);
......@@ -247,7 +247,7 @@ print_ip6_config (NMIPConfig *cfg6,
g_ptr_array_add (out.output_data, arr);
print_data_prepare_width (out.output_data);
print_data (nmc_config, &out);
print_data (nmc_config, out_indices, NULL, 0, &out);
return TRUE;
}
......@@ -275,7 +275,7 @@ print_dhcp4_config (NMDhcpConfig *dhcp4,
tmpl = nmc_fields_dhcp4_config;
tmpl_len = sizeof (nmc_fields_dhcp4_config);
out.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_DHCP4_CONFIG_ALL,
out_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 (out.output_data, arr);
......@@ -292,7 +292,7 @@ print_dhcp4_config (NMDhcpConfig *dhcp4,
g_ptr_array_add (out.output_data, arr);
print_data_prepare_width (out.output_data);
print_data (nmc_config, &out);
print_data (nmc_config, out_indices, NULL, 0, &out);
return TRUE;
}
......@@ -322,7 +322,7 @@ print_dhcp6_config (NMDhcpConfig *dhcp6,
tmpl = nmc_fields_dhcp6_config;
tmpl_len = sizeof (nmc_fields_dhcp6_config);
out.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_DHCP6_CONFIG_ALL,
out_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 (out.output_data, arr);
......@@ -339,7 +339,7 @@ print_dhcp6_config (NMDhcpConfig *dhcp6,
g_ptr_array_add (out.output_data, arr);
print_data_prepare_width (out.output_data);
print_data (nmc_config, &out);
print_data (nmc_config, out_indices, NULL, 0, &out);
return TRUE;
}
......
......@@ -629,20 +629,13 @@ quit (void)
g_main_loop_quit (loop); /* quit main loop */
}
static const char *
static char *
construct_header_name (const char *base, const char *spec)
{
static char header_name[128];
if (spec == NULL)
return base;
g_strlcpy (header_name, base, sizeof (header_name));
g_strlcat (header_name, " (", sizeof (header_name));
g_strlcat (header_name, spec, sizeof (header_name));
g_strlcat (header_name, ")", sizeof (header_name));
return g_strdup (base);
return header_name;
return g_strdup_printf ("%s (%s)", base, spec);
}
static const char *
......@@ -792,15 +785,16 @@ nmc_connection_profile_details (NMConnection *connection, NmCli *nmc, gboolean s
/* Main header */
{
NMC_OUTPUT_DATA_DEFINE_SCOPED (out);
gs_free char *header_name = NULL;
out.header_name = construct_header_name (base_hdr, nm_connection_get_id (connection));
out.indices = parse_output_fields (NMC_FIELDS_SETTINGS_NAMES_ALL,
header_name = construct_header_name (base_hdr, nm_connection_get_id (connection));
out_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, NMC_OF_FLAG_MAIN_HEADER_ONLY,
out.indices, out.header_name,
out.indent, nmc_fields_settings_names);
out_indices, header_name,
0, nmc_fields_settings_names);
}
/* Loop through the required settings and print them. */
......@@ -1221,15 +1215,16 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc)
/* Main header */
{
NMC_OUTPUT_DATA_DEFINE_SCOPED (out);
gs_free char *header_name = NULL;
out.header_name = construct_header_name (base_hdr, nm_active_connection_get_uuid (acon));
out.indices = parse_output_fields (NMC_FIELDS_CON_ACTIVE_DETAILS_ALL,
nmc_fields_con_active_details_groups, FALSE, NULL, NULL);
header_name = construct_header_name (base_hdr, nm_active_connection_get_uuid (acon));
out_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_OF_FLAG_MAIN_HEADER_ONLY,
out.indices, out.header_name,
out.indent, nmc_fields_con_active_details_groups);
out_indices, header_name,
0, nmc_fields_con_active_details_groups);
}
/* Loop through the groups and print them. */
......@@ -1249,7 +1244,7 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc)
/* Add field names */
tmpl = nmc_fields_con_active_details_general;
tmpl_len = sizeof (nmc_fields_con_active_details_general);
out.indices = parse_output_fields (group_fld ? group_fld : NMC_FIELDS_CON_ACTIVE_DETAILS_GENERAL_ALL,
out_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 (out.output_data, arr);
......@@ -1258,7 +1253,7 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc)
fill_output_active_connection (acon, out.output_data, TRUE, NMC_OF_FLAG_SECTION_PREFIX);
print_data_prepare_width (out.output_data);
print_data (&nmc->nmc_config, &out);
print_data (&nmc->nmc_config, out_indices, NULL, 0, &out);
was_output = TRUE;
}
......@@ -1320,7 +1315,7 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc)
tmpl = nmc_fields_con_active_details_vpn;
tmpl_len = sizeof (nmc_fields_con_active_details_vpn);
out.indices = parse_output_fields (group_fld ? group_fld : NMC_FIELDS_CON_ACTIVE_DETAILS_VPN_ALL,
out_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 (out.output_data, arr);
......@@ -1359,7 +1354,7 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc)
g_ptr_array_add (out.output_data, arr);
print_data_prepare_width (out.output_data);
print_data (&nmc->nmc_config, &out);
print_data (&nmc->nmc_config, out_indices, NULL, 0, &out);
was_output = TRUE;
}
}
......@@ -1814,13 +1809,11 @@ do_connections_show (NmCli *nmc, int argc, char **argv)
tmpl = nmc_fields_con_show;
tmpl_len = sizeof (nmc_fields_con_show);
out.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &err);
out_indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &err);
if (err)
goto finish;
/* Add headers */
out.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 (out.output_data, arr);
......@@ -1839,7 +1832,10 @@ do_connections_show (NmCli *nmc, int argc, char **argv)
g_ptr_array_free (sorted_cons, TRUE);
print_data_prepare_width (out.output_data);
print_data (&nmc->nmc_config, &out);
print_data (&nmc->nmc_config, out_indices,
active_only ? _("NetworkManager active profiles")
: _("NetworkManager connection profiles"),
0, &out);
} else {
gboolean new_line = FALSE;
gboolean without_fields = (nmc->required_fields == NULL);
......
This diff is collapsed.
......@@ -336,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);
out.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error);
out_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);
......@@ -354,7 +354,6 @@ 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);
out.header_name = 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 (out.output_data, arr);
......@@ -383,7 +382,9 @@ show_nm_status (NmCli *nmc, const char *pretty_header_name, const char *print_fl
g_ptr_array_add (out.output_data, arr);
print_data_prepare_width (out.output_data);
print_data (&nmc->nmc_config, &out);
print_data (&nmc->nmc_config, out_indices,
pretty_header_name ?: _("NetworkManager status"),
0, &out);
return TRUE;
}
......@@ -486,7 +487,7 @@ print_permissions (void *user_data)
tmpl = nmc_fields_nm_permissions;
tmpl_len = sizeof (nmc_fields_nm_permissions);
out.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error);
out_indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error);
if (error) {
g_string_printf (nmc->return_text, _("Error: 'general permissions': %s"), error->message);
......@@ -495,7 +496,6 @@ print_permissions (void *user_data)
return FALSE;
}
out.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 (out.output_data, arr);
......@@ -509,7 +509,10 @@ print_permissions (void *user_data)
g_ptr_array_add (out.output_data, arr);
}
print_data_prepare_width (out.output_data);
print_data (&nmc->nmc_config, &out);
print_data (&nmc->nmc_config,
out_indices,
_("NetworkManager permissions"),
0, &out);
quit ();
return G_SOURCE_REMOVE;
......@@ -598,7 +601,7 @@ show_general_logging (NmCli *nmc)
tmpl = nmc_fields_nm_logging;
tmpl_len = sizeof (nmc_fields_nm_logging);
out.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error);
out_indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error);
if (error) {
g_string_printf (nmc->return_text, _("Error: 'general logging': %s"), error->message);
......@@ -615,7 +618,6 @@ show_general_logging (NmCli *nmc)
return FALSE;
}
out.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 (out.output_data, arr);
......@@ -625,7 +627,9 @@ show_general_logging (NmCli *nmc)
g_ptr_array_add (out.output_data, arr);
print_data_prepare_width (out.output_data);
print_data (&nmc->nmc_config, &out);
print_data (&nmc->nmc_config, out_indices,
_("NetworkManager logging"),
0, &out);
return TRUE;
}
......
......@@ -144,9 +144,6 @@ typedef struct _NmcConfig {
typedef struct _NmcOutputData {
GPtrArray *output_data; /* GPtrArray of arrays of NmcOutputField structs - accumulates data for output */
GArray *indices; /* Array of field indices to the array of allowed fields */
const char *header_name; /* Name of the output */
int indent; /* Indent by this number of spaces */
} NmcOutputData;
/* NmCli - main structure */
......@@ -193,6 +190,7 @@ void nmc_exit (void);
void nmc_empty_output_fields (NmcOutputData *output_data);
#define NMC_OUTPUT_DATA_DEFINE_SCOPED(out) \
gs_unref_array GArray *out##_indices = NULL; \
nm_auto (nmc_empty_output_fields) NmcOutputData out = { \
.output_data = g_ptr_array_new_full (20, g_free), \
}
......
......@@ -1021,7 +1021,7 @@ 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);
out.indices = parse_output_fields (one_prop ?: (s_all = _all_properties (setting_info)),
out_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 (out.output_data, arr);
......@@ -1043,7 +1043,7 @@ setting_details (NMSetting *setting, NmCli *nmc, const char *one_prop, gboolean
g_ptr_array_add (out.output_data, arr);
print_data_prepare_width (out.output_data);
print_data (&nmc->nmc_config, &out);
print_data (&nmc->nmc_config, out_indices, NULL, 0, &out);
return TRUE;
}
......@@ -840,11 +840,6 @@ nmc_empty_output_fields (NmcOutputData *output_data)
/* Empty output_data array */
if (output_data->output_data->len > 0)
g_ptr_array_remove_range (output_data->output_data, 0, output_data->output_data->len);
if (output_data->indices) {
g_array_free (output_data->indices, TRUE);
output_data->indices = NULL;
}
}
static const char *
......@@ -1123,7 +1118,11 @@ print_data_prepare_width (GPtrArray *output_data)
}
void
print_data (const NmcConfig *nmc_config, const NmcOutputData *out)
print_data (const NmcConfig *nmc_config,
const GArray *indices,
const char *header_name,
int indent,
const NmcOutputData *out)
{
guint i;
......@@ -1131,8 +1130,8 @@ print_data (const NmcConfig *nmc_config, const NmcOutputData *out)
const NmcOutputField *field_values = g_ptr_array_index (out->output_data, i);
print_required_fields (nmc_config, field_values[0].flags,
out->indices, out->header_name,
out->indent, field_values);
indices, header_name,
indent, field_values);
}
}
......@@ -75,6 +75,10 @@ void print_required_fields (const NmcConfig *nmc_config,
int indent,
const NmcOutputField *field_values);
void print_data_prepare_width (GPtrArray *output_data);
void print_data (const NmcConfig *nmc_config, const NmcOutputData *out);
void print_data (const NmcConfig *nmc_config,
const GArray *indices,
const char *header_name,
int indent,
const NmcOutputData *out);
#endif /* NMC_UTILS_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment