Commit f973f084 authored by Thomas Haller's avatar Thomas Haller

cli: merge NmcPrintFields into NmcOutputData and pass it directly to print_required_fields()

parent ea700eec
......@@ -103,7 +103,7 @@ print_ip4_config (NMIPConfig *cfg4,
tmpl = nmc_fields_ip4_config;
tmpl_len = sizeof (nmc_fields_ip4_config);
out.print_fields.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);
......@@ -190,7 +190,7 @@ print_ip6_config (NMIPConfig *cfg6,
tmpl = nmc_fields_ip6_config;
tmpl_len = sizeof (nmc_fields_ip6_config);
out.print_fields.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);
......@@ -275,7 +275,7 @@ print_dhcp4_config (NMDhcpConfig *dhcp4,
tmpl = nmc_fields_dhcp4_config;
tmpl_len = sizeof (nmc_fields_dhcp4_config);
out.print_fields.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);
......@@ -322,7 +322,7 @@ print_dhcp6_config (NMDhcpConfig *dhcp6,
tmpl = nmc_fields_dhcp6_config;
tmpl_len = sizeof (nmc_fields_dhcp6_config);
out.print_fields.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);
......
......@@ -793,12 +793,14 @@ nmc_connection_profile_details (NMConnection *connection, NmCli *nmc, gboolean s
{
NMC_OUTPUT_DATA_DEFINE_SCOPED (out);
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,
out.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.print_fields, nmc_fields_settings_names);
print_required_fields (&nmc->nmc_config, NMC_OF_FLAG_MAIN_HEADER_ONLY,
out.indices, out.header_name,
out.indent, nmc_fields_settings_names);
}
/* Loop through the required settings and print them. */
......@@ -1220,12 +1222,14 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc)
{
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,
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);
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.print_fields, nmc_fields_con_active_details_groups);
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);
}
/* Loop through the groups and print them. */
......@@ -1245,7 +1249,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.print_fields.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);
......@@ -1316,7 +1320,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.print_fields.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);
......@@ -1810,13 +1814,13 @@ do_connections_show (NmCli *nmc, int argc, char **argv)
tmpl = nmc_fields_con_show;
tmpl_len = sizeof (nmc_fields_con_show);
out.print_fields.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.print_fields.header_name = active_only ? _("NetworkManager active profiles") :
_("NetworkManager connection profiles");
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);
......
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.print_fields.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,7 @@ 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.print_fields.header_name = pretty_header_name ? (char *) pretty_header_name : _("NetworkManager status");
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);
......@@ -486,7 +486,7 @@ print_permissions (void *user_data)
tmpl = nmc_fields_nm_permissions;
tmpl_len = sizeof (nmc_fields_nm_permissions);
out.print_fields.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 +495,7 @@ print_permissions (void *user_data)
return FALSE;
}
out.print_fields.header_name = _("NetworkManager permissions");
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);
......@@ -598,7 +598,7 @@ show_general_logging (NmCli *nmc)
tmpl = nmc_fields_nm_logging;
tmpl_len = sizeof (nmc_fields_nm_logging);
out.print_fields.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 +615,7 @@ show_general_logging (NmCli *nmc)
return FALSE;
}
out.print_fields.header_name = _("NetworkManager logging");
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);
......
......@@ -128,12 +128,6 @@ typedef struct _NmcOutputField {
const struct _NMMetaSettingInfoEditor *setting_info;
} NmcOutputField;
typedef struct {
GArray *indices; /* Array of field indices to the array of allowed fields */
char *header_name; /* Name of the output */
int indent; /* Indent by this number of spaces */
} NmcPrintFields;
typedef enum {
NMC_USE_COLOR_AUTO,
NMC_USE_COLOR_YES,
......@@ -150,7 +144,9 @@ typedef struct _NmcConfig {
typedef struct _NmcOutputData {
GPtrArray *output_data; /* GPtrArray of arrays of NmcOutputField structs - accumulates data for output */
NmcPrintFields print_fields; /* Structure with field indices to print */
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 */
......
......@@ -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.print_fields.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);
......
......@@ -841,9 +841,9 @@ nmc_empty_output_fields (NmcOutputData *output_data)
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->print_fields.indices) {
g_array_free (output_data->print_fields.indices, TRUE);
output_data->print_fields.indices = NULL;
if (output_data->indices) {
g_array_free (output_data->indices, TRUE);
output_data->indices = NULL;
}
}
......@@ -905,12 +905,17 @@ get_value_to_print (NmcColorOption color_option,
/*
* Print both headers or values of 'field_values' array.
* Entries to print and their order are specified via indices in
* 'nmc->print_fields.indices' array.
* 'nmc->indices' array.
* Various flags influencing the output of fields are set up in the first item
* of 'field_values' array.
*/
void
print_required_fields (const NmcConfig *nmc_config, NmcOfFlags of_flags, const NmcPrintFields *print_fields, const NmcOutputField *field_values)
print_required_fields (const NmcConfig *nmc_config,
NmcOfFlags of_flags,
const GArray *indices,
const char *header_name,
int indent,
const NmcOutputField *field_values)
{
GString *str;
int width1, width2;
......@@ -935,7 +940,7 @@ print_required_fields (const NmcConfig *nmc_config, NmcOfFlags of_flags, const N
/* --- Main header --- */
if (main_header && pretty) {
int header_width = nmc_string_screen_width (print_fields->header_name, NULL) + 4;
int header_width = nmc_string_screen_width (header_name, NULL) + 4;
if (multiline) {
table_width = header_width < ML_HEADER_WIDTH ? ML_HEADER_WIDTH : header_width;
......@@ -945,10 +950,10 @@ print_required_fields (const NmcConfig *nmc_config, NmcOfFlags of_flags, const N
line = g_strnfill (table_width, '=');
}
width1 = strlen (print_fields->header_name);
width2 = nmc_string_screen_width (print_fields->header_name, NULL);
width1 = strlen (header_name);
width2 = nmc_string_screen_width (header_name, NULL);
g_print ("%s\n", line);
g_print ("%*s\n", (table_width + width2)/2 + width1 - width2, print_fields->header_name);
g_print ("%*s\n", (table_width + width2)/2 + width1 - width2, header_name);
g_print ("%s\n", line);
g_free (line);
}
......@@ -965,9 +970,9 @@ print_required_fields (const NmcConfig *nmc_config, NmcOfFlags of_flags, const N
if (multiline) {
for (i = 0; i < print_fields->indices->len; i++) {
for (i = 0; i < indices->len; i++) {
char *tmp;
int idx = g_array_index (print_fields->indices, int, i);
int idx = g_array_index (indices, int, i);
gboolean is_array = field_values[idx].value_is_array;
/* section prefix can't be an array */
......@@ -1029,8 +1034,8 @@ print_required_fields (const NmcConfig *nmc_config, NmcOfFlags of_flags, const N
str = g_string_new (NULL);
for (i = 0; i < print_fields->indices->len; i++) {
int idx = g_array_index (print_fields->indices, int, i);
for (i = 0; i < indices->len; i++) {
int idx = g_array_index (indices, int, i);
gs_free char *val_to_free = NULL;
const char *value = get_value_to_print (nmc_config->use_colors, (NmcOutputField *) field_values+idx, field_names,
not_set_str, &val_to_free);
......@@ -1060,8 +1065,8 @@ print_required_fields (const NmcConfig *nmc_config, NmcOfFlags of_flags, const N
/* Print actual values */
if (str->len > 0) {
g_string_truncate (str, str->len-1); /* Chop off last column separator */
if (print_fields->indent > 0) {
indent_str = g_strnfill (print_fields->indent, ' ');
if (indent > 0) {
indent_str = g_strnfill (indent, ' ');
g_string_prepend (str, indent_str);
g_free (indent_str);
}
......@@ -1126,7 +1131,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->print_fields, field_values);
out->indices, out->header_name,
out->indent, field_values);
}
}
......@@ -68,7 +68,12 @@ GArray *parse_output_fields (const char *fields_str,
char *nmc_get_allowed_fields (const NmcOutputField fields_array[], int group_idx);
NmcOutputField *nmc_dup_fields_array (NmcOutputField fields[], size_t size, guint32 flags);
void nmc_empty_output_fields (NmcOutputData *output_data);
void print_required_fields (const NmcConfig *nmc_config, NmcOfFlags of_flags, const NmcPrintFields *print_fields, const NmcOutputField field_values[]);
void print_required_fields (const NmcConfig *nmc_config,
NmcOfFlags of_flags,
const GArray *indices,
const char *header_name,
int indent,
const NmcOutputField *field_values);
void print_data_prepare_width (GPtrArray *output_data);
void print_data (const NmcConfig *nmc_config, const NmcOutputData *out);
......
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