Commit 1d1cfd77 authored by Jiří Klimeš's avatar Jiří Klimeš

cli: adapt the code for the new flags and printing facilities

The data are added to output_data at first, and then they are printed all at
once using print_data(), that takes care of proper alignment and display.

The static 'width' values defined in NmcOutputField columns are not used now,
but dynamically computed maximal widths override them.
parent 7fe4345d
......@@ -81,17 +81,17 @@ print_ip4_config (NMIP4Config *cfg4, NmCli *nmc, const char *group_prefix)
char **domain_arr = NULL;
char **wins_arr = NULL;
int i = 0;
guint32 mode_flag = (nmc->print_output == NMC_PRINT_PRETTY) ? NMC_PF_FLAG_PRETTY : (nmc->print_output == NMC_PRINT_TERSE) ? NMC_PF_FLAG_TERSE : 0;
guint32 multiline_flag = nmc->multiline_output ? NMC_PF_FLAG_MULTILINE : 0;
guint32 escape_flag = nmc->escape_values ? NMC_PF_FLAG_ESCAPE : 0;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
if (cfg4 == NULL)
return FALSE;
nmc->allowed_fields = nmc_fields_ip4_config;
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.indices = parse_output_fields (NMC_FIELDS_IP4_CONFIG_ALL, nmc->allowed_fields, NULL);
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
tmpl = nmc_fields_ip4_config;
tmpl_len = sizeof (nmc_fields_ip4_config);
nmc->print_fields.indices = parse_output_fields (NMC_FIELDS_IP4_CONFIG_ALL, tmpl, NULL);
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (nmc->output_data, arr);
/* addresses */
list = (GSList *) nm_ip4_config_get_addresses (cfg4);
......@@ -146,7 +146,7 @@ print_ip4_config (NMIP4Config *cfg4, NmCli *nmc, const char *group_prefix)
if (ptr_array) {
domain_arr = g_new (char *, ptr_array->len + 1);
for (i = 0; i < ptr_array->len; i++)
domain_arr[i] = g_ptr_array_index (ptr_array, i);
domain_arr[i] = g_strdup (g_ptr_array_index (ptr_array, i));
domain_arr[i] = NULL;
}
......@@ -161,21 +161,19 @@ print_ip4_config (NMIP4Config *cfg4, NmCli *nmc, const char *group_prefix)
wins_arr[i] = NULL;
}
set_val_str (nmc->allowed_fields, 0, (char *) group_prefix);
set_val_arr (nmc->allowed_fields, 1, addr_arr);
set_val_arr (nmc->allowed_fields, 2, route_arr);
set_val_arr (nmc->allowed_fields, 3, dns_arr);
set_val_arr (nmc->allowed_fields, 4, domain_arr);
set_val_arr (nmc->allowed_fields, 5, wins_arr);
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, addr_arr);
set_val_arr (arr, 2, route_arr);
set_val_arr (arr, 3, dns_arr);
set_val_arr (arr, 4, domain_arr);
set_val_arr (arr, 5, wins_arr);
g_ptr_array_add (nmc->output_data, arr);
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_SECTION_PREFIX;
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print values */
print_data (nmc); /* Print all data */
g_strfreev (addr_arr);
g_strfreev (route_arr);
g_strfreev (dns_arr);
g_free (domain_arr);
g_strfreev (wins_arr);
/* Remove any previous data */
nmc_empty_output_fields (nmc);
return TRUE;
}
......@@ -190,17 +188,17 @@ print_ip6_config (NMIP6Config *cfg6, NmCli *nmc, const char *group_prefix)
char **dns_arr = NULL;
char **domain_arr = NULL;
int i = 0;
guint32 mode_flag = (nmc->print_output == NMC_PRINT_PRETTY) ? NMC_PF_FLAG_PRETTY : (nmc->print_output == NMC_PRINT_TERSE) ? NMC_PF_FLAG_TERSE : 0;
guint32 multiline_flag = nmc->multiline_output ? NMC_PF_FLAG_MULTILINE : 0;
guint32 escape_flag = nmc->escape_values ? NMC_PF_FLAG_ESCAPE : 0;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
if (cfg6 == NULL)
return FALSE;
nmc->allowed_fields = nmc_fields_ip6_config;
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.indices = parse_output_fields (NMC_FIELDS_IP6_CONFIG_ALL, nmc->allowed_fields, NULL);
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
tmpl = nmc_fields_ip6_config;
tmpl_len = sizeof (nmc_fields_ip6_config);
nmc->print_fields.indices = parse_output_fields (NMC_FIELDS_IP6_CONFIG_ALL, tmpl, NULL);
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (nmc->output_data, arr);
/* addresses */
list = (GSList *) nm_ip6_config_get_addresses (cfg6);
......@@ -254,24 +252,23 @@ print_ip6_config (NMIP6Config *cfg6, NmCli *nmc, const char *group_prefix)
if (ptr_array) {
domain_arr = g_new (char *, ptr_array->len + 1);
for (i = 0; i < ptr_array->len; i++)
domain_arr[i] = g_ptr_array_index (ptr_array, i);
domain_arr[i] = g_strdup (g_ptr_array_index (ptr_array, i));
domain_arr[i] = NULL;
}
set_val_str (nmc->allowed_fields, 0, (char *) group_prefix);
set_val_arr (nmc->allowed_fields, 1, addr_arr);
set_val_arr (nmc->allowed_fields, 2, route_arr);
set_val_arr (nmc->allowed_fields, 3, dns_arr);
set_val_arr (nmc->allowed_fields, 4, domain_arr);
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, addr_arr);
set_val_arr (arr, 2, route_arr);
set_val_arr (arr, 3, dns_arr);
set_val_arr (arr, 4, domain_arr);
g_ptr_array_add (nmc->output_data, arr);
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_SECTION_PREFIX;
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print values */
print_data (nmc); /* Print all data */
g_strfreev (addr_arr);
g_strfreev (route_arr);
g_strfreev (dns_arr);
g_free (domain_arr);
/* Remove any previous data */
nmc_empty_output_fields (nmc);
return TRUE;
}
......@@ -280,9 +277,8 @@ gboolean
print_dhcp4_config (NMDHCP4Config *dhcp4, NmCli *nmc, const char *group_prefix)
{
GHashTable *table;
guint32 mode_flag = (nmc->print_output == NMC_PRINT_PRETTY) ? NMC_PF_FLAG_PRETTY : (nmc->print_output == NMC_PRINT_TERSE) ? NMC_PF_FLAG_TERSE : 0;
guint32 multiline_flag = nmc->multiline_output ? NMC_PF_FLAG_MULTILINE : 0;
guint32 escape_flag = nmc->escape_values ? NMC_PF_FLAG_ESCAPE : 0;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
if (dhcp4 == NULL)
return FALSE;
......@@ -294,10 +290,11 @@ print_dhcp4_config (NMDHCP4Config *dhcp4, NmCli *nmc, const char *group_prefix)
char **options_arr = NULL;
int i = 0;
nmc->allowed_fields = nmc_fields_dhcp4_config;
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.indices = parse_output_fields (NMC_FIELDS_DHCP4_CONFIG_ALL, nmc->allowed_fields, NULL);
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
tmpl = nmc_fields_dhcp4_config;
tmpl_len = sizeof (nmc_fields_dhcp4_config);
nmc->print_fields.indices = parse_output_fields (NMC_FIELDS_DHCP4_CONFIG_ALL, tmpl, NULL);
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (nmc->output_data, arr);
options_arr = g_new (char *, g_hash_table_size (table) + 1);
g_hash_table_iter_init (&table_iter, table);
......@@ -305,13 +302,15 @@ print_dhcp4_config (NMDHCP4Config *dhcp4, NmCli *nmc, const char *group_prefix)
options_arr[i++] = g_strdup_printf ("%s = %s", (char *) key, (char *) value);
options_arr[i] = NULL;
set_val_str (nmc->allowed_fields, 0, (char *) group_prefix);
set_val_arr (nmc->allowed_fields, 1, options_arr);
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->output_data, arr);
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_SECTION_PREFIX;
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print values */
print_data (nmc); /* Print all data */
g_strfreev (options_arr);
/* Remove any previous data */
nmc_empty_output_fields (nmc);
return TRUE;
}
......@@ -322,9 +321,8 @@ gboolean
print_dhcp6_config (NMDHCP6Config *dhcp6, NmCli *nmc, const char *group_prefix)
{
GHashTable *table;
guint32 mode_flag = (nmc->print_output == NMC_PRINT_PRETTY) ? NMC_PF_FLAG_PRETTY : (nmc->print_output == NMC_PRINT_TERSE) ? NMC_PF_FLAG_TERSE : 0;
guint32 multiline_flag = nmc->multiline_output ? NMC_PF_FLAG_MULTILINE : 0;
guint32 escape_flag = nmc->escape_values ? NMC_PF_FLAG_ESCAPE : 0;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
if (dhcp6 == NULL)
return FALSE;
......@@ -336,10 +334,11 @@ print_dhcp6_config (NMDHCP6Config *dhcp6, NmCli *nmc, const char *group_prefix)
char **options_arr = NULL;
int i = 0;
nmc->allowed_fields = nmc_fields_dhcp6_config;
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.indices = parse_output_fields (NMC_FIELDS_DHCP6_CONFIG_ALL, nmc->allowed_fields, NULL);
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
tmpl = nmc_fields_dhcp6_config;
tmpl_len = sizeof (nmc_fields_dhcp6_config);
nmc->print_fields.indices = parse_output_fields (NMC_FIELDS_DHCP6_CONFIG_ALL, tmpl, NULL);
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (nmc->output_data, arr);
options_arr = g_new (char *, g_hash_table_size (table) + 1);
g_hash_table_iter_init (&table_iter, table);
......@@ -347,13 +346,15 @@ print_dhcp6_config (NMDHCP6Config *dhcp6, NmCli *nmc, const char *group_prefix)
options_arr[i++] = g_strdup_printf ("%s = %s", (char *) key, (char *) value);
options_arr[i] = NULL;
set_val_str (nmc->allowed_fields, 0, (char *) group_prefix);
set_val_arr (nmc->allowed_fields, 1, options_arr);
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->output_data, arr);
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_SECTION_PREFIX;
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print values */
print_data (nmc); /* Print all data */
g_strfreev (options_arr);
/* Remove any previous data */
nmc_empty_output_fields (nmc);
return TRUE;
}
......
This diff is collapsed.
This diff is collapsed.
......@@ -167,9 +167,8 @@ show_nm_status (NmCli *nmc, const char *pretty_header_name, const char *print_fl
const char *fields_str;
const char *fields_all = print_flds ? print_flds : NMC_FIELDS_NM_STATUS_ALL;
const char *fields_common = print_flds ? print_flds : NMC_FIELDS_NM_STATUS_COMMON;
guint32 mode_flag = (nmc->print_output == NMC_PRINT_PRETTY) ? NMC_PF_FLAG_PRETTY : (nmc->print_output == NMC_PRINT_TERSE) ? NMC_PF_FLAG_TERSE : 0;
guint32 multiline_flag = nmc->multiline_output ? NMC_PF_FLAG_MULTILINE : 0;
guint32 escape_flag = nmc->escape_values ? NMC_PF_FLAG_ESCAPE : 0;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0)
fields_str = fields_common;
......@@ -178,8 +177,9 @@ show_nm_status (NmCli *nmc, const char *pretty_header_name, const char *print_fl
else
fields_str = nmc->required_fields;
nmc->allowed_fields = nmc_fields_nm_status;
nmc->print_fields.indices = parse_output_fields (fields_str, nmc->allowed_fields, &error);
tmpl = nmc_fields_nm_status;
tmpl_len = sizeof (nmc_fields_nm_status);
nmc->print_fields.indices = parse_output_fields (fields_str, tmpl, &error);
if (error) {
if (error->code == 0)
......@@ -218,25 +218,26 @@ show_nm_status (NmCli *nmc, const char *pretty_header_name, const char *print_fl
#endif
}
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.header_name = pretty_header_name ? (char *) pretty_header_name : _("NetworkManager status");
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
nmc->allowed_fields[0].value = nm_running ? _("running") : _("not running");
nmc->allowed_fields[1].value = nm_running ? (char *) nm_client_get_version (nmc->client) : _("unknown");
nmc->allowed_fields[2].value = (char *) nm_state_to_string (state);
nmc->allowed_fields[3].value = (char *) net_enabled_str;
nmc->allowed_fields[4].value = (char *) wireless_hw_enabled_str;
nmc->allowed_fields[5].value = (char *) wireless_enabled_str;
nmc->allowed_fields[6].value = (char *) wwan_hw_enabled_str;
nmc->allowed_fields[7].value = (char *) wwan_enabled_str;
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (nmc->output_data, arr);
arr = nmc_dup_fields_array (tmpl, tmpl_len, 0);
set_val_strc (arr, 0, nm_running ? _("running") : _("not running"));
set_val_strc (arr, 1, nm_running ? nm_client_get_version (nmc->client) : _("unknown"));
set_val_strc (arr, 2, nm_state_to_string (state));
set_val_strc (arr, 3, net_enabled_str);
set_val_strc (arr, 4, wireless_hw_enabled_str);
set_val_strc (arr, 5, wireless_enabled_str);
set_val_strc (arr, 6, wwan_hw_enabled_str);
set_val_strc (arr, 7, wwan_enabled_str);
#if WITH_WIMAX
nmc->allowed_fields[8].value = (char *) wimax_hw_enabled_str;
nmc->allowed_fields[9].value = (char *) wimax_enabled_str;
set_val_strc (arr, 8, wimax_hw_enabled_str);
set_val_strc (arr, 9, wimax_enabled_str);
#endif
g_ptr_array_add (nmc->output_data, arr);
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag;
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print values */
print_data (nmc); /* Print all data */
return TRUE;
}
......@@ -308,9 +309,8 @@ show_nm_permissions (NmCli *nmc)
const char *fields_str;
const char *fields_all = NMC_FIELDS_NM_PERMISSIONS_ALL;
const char *fields_common = NMC_FIELDS_NM_PERMISSIONS_COMMON;
guint32 mode_flag = (nmc->print_output == NMC_PRINT_PRETTY) ? NMC_PF_FLAG_PRETTY : (nmc->print_output == NMC_PRINT_TERSE) ? NMC_PF_FLAG_TERSE : 0;
guint32 multiline_flag = nmc->multiline_output ? NMC_PF_FLAG_MULTILINE : 0;
guint32 escape_flag = nmc->escape_values ? NMC_PF_FLAG_ESCAPE : 0;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0)
fields_str = fields_common;
......@@ -319,8 +319,9 @@ show_nm_permissions (NmCli *nmc)
else
fields_str = nmc->required_fields;
nmc->allowed_fields = nmc_fields_nm_permissions;
nmc->print_fields.indices = parse_output_fields (fields_str, nmc->allowed_fields, &error);
tmpl = nmc_fields_nm_permissions;
tmpl_len = sizeof (nmc_fields_nm_permissions);
nmc->print_fields.indices = parse_output_fields (fields_str, tmpl, &error);
if (error) {
if (error->code == 0)
......@@ -341,18 +342,19 @@ show_nm_permissions (NmCli *nmc)
return FALSE;
}
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.header_name = _("NetworkManager permissions");
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (nmc->output_data, arr);
for (perm = NM_CLIENT_PERMISSION_NONE + 1; perm <= NM_CLIENT_PERMISSION_LAST; perm++) {
NMClientPermissionResult perm_result = nm_client_get_permission_result (nmc->client, perm);
set_val_str (nmc->allowed_fields, 0, (char *) permission_to_string (perm));
set_val_str (nmc->allowed_fields, 1, (char *) permission_result_to_string (perm_result));
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag;
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print values */
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->output_data, arr);
}
print_data (nmc); /* Print all data */
return TRUE;
}
......@@ -366,9 +368,8 @@ show_general_logging (NmCli *nmc)
const char *fields_str;
const char *fields_all = NMC_FIELDS_NM_LOGGING_ALL;
const char *fields_common = NMC_FIELDS_NM_LOGGING_COMMON;
guint32 mode_flag = (nmc->print_output == NMC_PRINT_PRETTY) ? NMC_PF_FLAG_PRETTY : (nmc->print_output == NMC_PRINT_TERSE) ? NMC_PF_FLAG_TERSE : 0;
guint32 multiline_flag = nmc->multiline_output ? NMC_PF_FLAG_MULTILINE : 0;
guint32 escape_flag = nmc->escape_values ? NMC_PF_FLAG_ESCAPE : 0;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0)
fields_str = fields_common;
......@@ -377,8 +378,10 @@ show_general_logging (NmCli *nmc)
else
fields_str = nmc->required_fields;
nmc->allowed_fields = nmc_fields_nm_logging;
nmc->print_fields.indices = parse_output_fields (fields_str, nmc->allowed_fields, &error);
tmpl = nmc_fields_nm_logging;
tmpl_len = sizeof (nmc_fields_nm_logging);
nmc->print_fields.indices = parse_output_fields (fields_str, tmpl, &error);
if (error) {
if (error->code == 0)
g_string_printf (nmc->return_text, _("Error: 'general logging': %s"), error->message);
......@@ -399,17 +402,17 @@ show_general_logging (NmCli *nmc)
return FALSE;
}
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.header_name = _("NetworkManager logging");
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (nmc->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->output_data, arr);
set_val_str (nmc->allowed_fields, 0, level);
set_val_str (nmc->allowed_fields, 1, domains);
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag;
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print values */
print_data (nmc); /* Print all data */
g_free (level);
g_free (domains);
return TRUE;
}
......
This diff is collapsed.
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