Commit 85acdd70 authored by Thomas Haller's avatar Thomas Haller

cli: split print_data() in a part with and without side-effects

To better understand which part of the code have side effects,
split print_data() in a part that mutilates the input array
and a part that only prints.
parent 2ea670ab
......@@ -162,7 +162,8 @@ print_ip4_config (NMIPConfig *cfg4,
set_val_arr (arr, 6, wins_arr);
g_ptr_array_add (nmc->out.output_data, arr);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
nmc_empty_output_fields (&nmc->out);
......@@ -245,7 +246,8 @@ print_ip6_config (NMIPConfig *cfg6,
set_val_arr (arr, 5, domain_arr);
g_ptr_array_add (nmc->out.output_data, arr);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
nmc_empty_output_fields (&nmc->out);
......@@ -290,7 +292,8 @@ print_dhcp4_config (NMDhcpConfig *dhcp4,
set_val_arr (arr, 1, options_arr);
g_ptr_array_add (nmc->out.output_data, arr);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
nmc_empty_output_fields (&nmc->out);
......@@ -337,7 +340,8 @@ print_dhcp6_config (NMDhcpConfig *dhcp6,
set_val_arr (arr, 1, options_arr);
g_ptr_array_add (nmc->out.output_data, arr);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
nmc_empty_output_fields (&nmc->out);
......
......@@ -1248,7 +1248,8 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc)
/* Fill in values */
fill_output_active_connection (acon, nmc, TRUE, NMC_OF_FLAG_SECTION_PREFIX);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
was_output = TRUE;
}
......@@ -1347,7 +1348,8 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc)
set_val_arr (arr, 6, vpn_data_array);
g_ptr_array_add (nmc->out.output_data, arr);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
was_output = TRUE;
}
}
......@@ -1829,7 +1831,8 @@ do_connections_show (NmCli *nmc, int argc, char **argv)
fill_output_connection (sorted_cons->pdata[i], nmc, active_only);
g_ptr_array_free (sorted_cons, TRUE);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
} else {
gboolean new_line = FALSE;
gboolean without_fields = (nmc->required_fields == NULL);
......
......@@ -984,7 +984,8 @@ print_bond_bridge_info (NMDevice *device,
set_val_str (arr, 1, slaves_str->str);
g_ptr_array_add (nmc->out.output_data, arr);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
g_string_free (slaves_str, FALSE);
nmc_empty_output_fields (&nmc->out);
......@@ -1054,7 +1055,8 @@ print_team_info (NMDevice *device,
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);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
g_string_free (slaves_str, FALSE);
nmc_empty_output_fields (&nmc->out);
......@@ -1170,7 +1172,8 @@ show_device_info (NMDevice *device, NmCli *nmc)
set_val_strc (arr, 24, nmc_device_metered_to_string (nm_device_get_metered (device)));
g_ptr_array_add (nmc->out.output_data, arr);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
was_output = TRUE;
}
......@@ -1202,7 +1205,8 @@ show_device_info (NMDevice *device, NmCli *nmc)
set_val_strc (arr, 3, (caps & NM_DEVICE_CAP_IS_SOFTWARE) ? _("yes") : _("no"));
g_ptr_array_add (nmc->out.output_data, arr);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
was_output = TRUE;
}
......@@ -1240,7 +1244,8 @@ show_device_info (NMDevice *device, NmCli *nmc)
((wcaps & NM_WIFI_DEVICE_CAP_FREQ_5GHZ) ? _("yes") : _("no")));
g_ptr_array_add (nmc->out.output_data, arr);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
was_output = TRUE;
}
......@@ -1268,7 +1273,8 @@ show_device_info (NMDevice *device, NmCli *nmc)
g_ptr_array_foreach ((GPtrArray *) aps, fill_output_access_point, (gpointer) info);
g_ptr_array_free (aps, FALSE);
g_free (info);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
was_output = TRUE;
}
} else if (NM_IS_DEVICE_ETHERNET (device)) {
......@@ -1288,7 +1294,8 @@ show_device_info (NMDevice *device, NmCli *nmc)
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);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
was_output = TRUE;
}
}
......@@ -1352,7 +1359,8 @@ show_device_info (NMDevice *device, NmCli *nmc)
set_val_str (arr, 2, vlan_id_str);
g_ptr_array_add (nmc->out.output_data, arr);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
was_output = TRUE;
}
......@@ -1372,7 +1380,8 @@ show_device_info (NMDevice *device, NmCli *nmc)
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);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
was_output = TRUE;
}
}
......@@ -1421,7 +1430,8 @@ show_device_info (NMDevice *device, NmCli *nmc)
set_val_arr (arr, 2, (ac_arr));
g_ptr_array_add (nmc->out.output_data, arr);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
g_string_free (ac_paths_str, FALSE);
was_output = TRUE;
......@@ -1529,7 +1539,8 @@ do_devices_status (NmCli *nmc, int argc, char **argv)
for (i = 0; devices[i]; i++)
fill_output_device_status (devices[i], nmc);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
g_free (devices);
......@@ -2494,7 +2505,8 @@ show_access_point_info (NMDevice *device, NmCli *nmc)
g_ptr_array_foreach ((GPtrArray *) aps, fill_output_access_point, (gpointer) info);
g_ptr_array_free (aps, FALSE);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
nmc_empty_output_fields (&nmc->out);
g_free (info);
}
......@@ -2724,7 +2736,8 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv)
fill_output_access_point (ap, info);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
g_free (info);
} else {
show_access_point_info (device, nmc);
......@@ -2784,7 +2797,8 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv)
}
if (empty_line)
g_print ("\n"); /* Empty line between devices' APs */
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
nmc_empty_output_fields (&nmc->out);
empty_line = TRUE;
}
......@@ -3732,7 +3746,8 @@ show_device_lldp_list (NMDevice *device, NmCli *nmc, char *fields_str, int *coun
g_ptr_array_add (nmc->out.output_data, arr);
}
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
nmc_empty_output_fields (&nmc->out);
return neighbors->len;
......
......@@ -381,7 +381,8 @@ show_nm_status (NmCli *nmc, const char *pretty_header_name, const char *print_fl
g_ptr_array_add (nmc->out.output_data, arr);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
return TRUE;
}
......@@ -504,7 +505,8 @@ print_permissions (void *user_data)
set_val_strc (arr, 1, permission_result_to_string (perm_result));
g_ptr_array_add (nmc->out.output_data, arr);
}
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
quit ();
return G_SOURCE_REMOVE;
......@@ -617,7 +619,8 @@ show_general_logging (NmCli *nmc)
set_val_str (arr, 1, domains);
g_ptr_array_add (nmc->out.output_data, arr);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
return TRUE;
}
......
......@@ -918,7 +918,8 @@ setting_details (NMSetting *setting, NmCli *nmc, const char *one_prop, gboolean
g_ptr_array_add (nmc->out.output_data, arr);
print_data (&nmc->nmc_config, &nmc->out.print_fields, nmc->out.output_data);
print_data_prepare_width (nmc->out.output_data);
print_data (&nmc->nmc_config, &nmc->out);
return TRUE;
}
......@@ -1033,16 +1033,8 @@ print_required_fields (const NmcConfig *nmc_config, const NmcPrintFields *print_
g_string_free (str, TRUE);
}
/*
* Print nmc->output_data
*
* It first finds out maximal string length in columns and fill the value to
* 'width' member of NmcOutputField, so that columns in tabular output are
* properly aligned. Then each object (row in tabular) is printed using
* print_required_fields() function.
*/
void
print_data (const NmcConfig *nmc_config, const NmcPrintFields *print_fields, GPtrArray *output_data)
print_data_prepare_width (GPtrArray *output_data)
{
int i, j;
size_t len;
......@@ -1078,11 +1070,16 @@ print_data (const NmcConfig *nmc_config, const NmcPrintFields *print_fields, GPt
row[i].width = max_width + 1;
}
}
}
void
print_data (const NmcConfig *nmc_config, const NmcOutputData *out)
{
guint i;
/* Now we can print the data. */
for (i = 0; i < output_data->len; i++) {
row = g_ptr_array_index (output_data, i);
print_required_fields (nmc_config, print_fields, row);
for (i = 0; i < out->output_data->len; i++) {
print_required_fields (nmc_config, &out->print_fields,
g_ptr_array_index (out->output_data, i));
}
}
......@@ -69,7 +69,7 @@ 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, const NmcPrintFields *print_fields, const NmcOutputField field_values[]);
void print_data (const NmcConfig *nmc_config, const NmcPrintFields *print_fields, GPtrArray *output_data);
void print_data_prepare_width (GPtrArray *output_data);
void print_data (const NmcConfig *nmc_config, 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