Commit bfb9fd0d authored by Thomas Haller's avatar Thomas Haller

cli: split tracking of meta data out of NmcOutputField

When generating output data, nmcli iterates over a list of
property-descriptors (nmc_fields_ip4_config), creates an intermediate
array (output_data) and finally prints it.

However, previously both the meta data (nmc_fields_ip4_config) and
the intermediate format use the same type NmcOutputField. This means,
certain fields are relevant to describe a property, and other fields
are output/formatting fields.

Split this up. Now, the meta data is tracked in form of an NMMetaAbstractInfo
lists. This separates the information about properties from intermediate steps
during creation of the output.

Note that currently functions like print_ip4_config() still have all the
knowledge about how to generate the output. That is wrong, instead, the
meta data (NMMetaAbstractInfo) should describe how to create the output
and then all those functions could be replaced. This means, later we want
to add more knowledge to the NMMetaAbstractInfo, so it is important to
keep them separate from NmcOutputField.
parent 6a489199
......@@ -37,49 +37,38 @@
extern GMainLoop *loop;
#define OUTPUT_FIELD_WITH_NAME(n) { .name = N_ (n), }
/* Available fields for IPv4 group */
NmcOutputField nmc_fields_ip4_config[] = {
OUTPUT_FIELD_WITH_NAME ("GROUP"), /* 0 */
OUTPUT_FIELD_WITH_NAME ("ADDRESS"), /* 1 */
OUTPUT_FIELD_WITH_NAME ("GATEWAY"), /* 2 */
OUTPUT_FIELD_WITH_NAME ("ROUTE"), /* 3 */
OUTPUT_FIELD_WITH_NAME ("DNS"), /* 4 */
OUTPUT_FIELD_WITH_NAME ("DOMAIN"), /* 5 */
OUTPUT_FIELD_WITH_NAME ("WINS"), /* 6 */
{ 0 }
const NmcMetaGenericInfo *const nmc_fields_ip4_config[] = {
NMC_META_GENERIC ("GROUP"), /* 0 */
NMC_META_GENERIC ("ADDRESS"), /* 1 */
NMC_META_GENERIC ("GATEWAY"), /* 2 */
NMC_META_GENERIC ("ROUTE"), /* 3 */
NMC_META_GENERIC ("DNS"), /* 4 */
NMC_META_GENERIC ("DOMAIN"), /* 5 */
NMC_META_GENERIC ("WINS"), /* 6 */
NULL,
};
#define NMC_FIELDS_IP4_CONFIG_ALL "GROUP,ADDRESS,GATEWAY,ROUTE,DNS,DOMAIN,WINS"
/* Available fields for DHCPv4 group */
NmcOutputField nmc_fields_dhcp4_config[] = {
OUTPUT_FIELD_WITH_NAME ("GROUP"), /* 0 */
OUTPUT_FIELD_WITH_NAME ("OPTION"), /* 1 */
{ 0 }
const NmcMetaGenericInfo *const nmc_fields_dhcp4_config[] = {
NMC_META_GENERIC ("GROUP"), /* 0 */
NMC_META_GENERIC ("OPTION"), /* 1 */
NULL,
};
#define NMC_FIELDS_DHCP4_CONFIG_ALL "GROUP,OPTION"
/* Available fields for IPv6 group */
NmcOutputField nmc_fields_ip6_config[] = {
OUTPUT_FIELD_WITH_NAME ("GROUP"), /* 0 */
OUTPUT_FIELD_WITH_NAME ("ADDRESS"), /* 1 */
OUTPUT_FIELD_WITH_NAME ("GATEWAY"), /* 2 */
OUTPUT_FIELD_WITH_NAME ("ROUTE"), /* 3 */
OUTPUT_FIELD_WITH_NAME ("DNS"), /* 4 */
OUTPUT_FIELD_WITH_NAME ("DOMAIN"), /* 5 */
{ 0 }
};
#define NMC_FIELDS_IP6_CONFIG_ALL "GROUP,ADDRESS,GATEWAY,ROUTE,DNS,DOMAIN"
/* Available fields for DHCPv6 group */
NmcOutputField nmc_fields_dhcp6_config[] = {
OUTPUT_FIELD_WITH_NAME ("GROUP"), /* 0 */
OUTPUT_FIELD_WITH_NAME ("OPTION"), /* 1 */
{ 0 }
const NmcMetaGenericInfo *const nmc_fields_ip6_config[] = {
NMC_META_GENERIC ("GROUP"), /* 0 */
NMC_META_GENERIC ("ADDRESS"), /* 1 */
NMC_META_GENERIC ("GATEWAY"), /* 2 */
NMC_META_GENERIC ("ROUTE"), /* 3 */
NMC_META_GENERIC ("DNS"), /* 4 */
NMC_META_GENERIC ("DOMAIN"), /* 5 */
NULL,
};
#define NMC_FIELDS_DHCP6_CONFIG_ALL "GROUP,OPTION"
const NmcMetaGenericInfo *const nmc_fields_dhcp6_config[] = {
NMC_META_GENERIC ("GROUP"), /* 0 */
NMC_META_GENERIC ("OPTION"), /* 1 */
NULL,
};
gboolean
print_ip4_config (NMIPConfig *cfg4,
......@@ -94,18 +83,17 @@ print_ip4_config (NMIPConfig *cfg4,
char **domain_arr = NULL;
char **wins_arr = NULL;
int i = 0;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
const NMMetaAbstractInfo *const*tmpl;
NmcOutputField *arr;
NMC_OUTPUT_DATA_DEFINE_SCOPED (out);
if (cfg4 == NULL)
return FALSE;
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,
tmpl = (const NMMetaAbstractInfo *const*) nmc_fields_ip4_config;
out_indices = parse_output_fields (one_field,
tmpl, FALSE, NULL, NULL);
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES);
arr = nmc_dup_fields_array (tmpl, NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (out.output_data, arr);
/* addresses */
......@@ -153,7 +141,7 @@ print_ip4_config (NMIPConfig *cfg4,
/* WINS */
wins_arr = g_strdupv ((char **) nm_ip_config_get_wins_servers (cfg4));
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX);
arr = nmc_dup_fields_array (tmpl, NMC_OF_FLAG_SECTION_PREFIX);
set_val_strc (arr, 0, group_prefix);
set_val_arr (arr, 1, addr_arr);
set_val_strc (arr, 2, nm_ip_config_get_gateway (cfg4));
......@@ -181,18 +169,17 @@ print_ip6_config (NMIPConfig *cfg6,
char **dns_arr = NULL;
char **domain_arr = NULL;
int i = 0;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
const NMMetaAbstractInfo *const*tmpl;
NmcOutputField *arr;
NMC_OUTPUT_DATA_DEFINE_SCOPED (out);
if (cfg6 == NULL)
return FALSE;
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,
tmpl = (const NMMetaAbstractInfo *const*) nmc_fields_ip6_config;
out_indices = parse_output_fields (one_field,
tmpl, FALSE, NULL, NULL);
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES);
arr = nmc_dup_fields_array (tmpl, NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (out.output_data, arr);
/* addresses */
......@@ -237,7 +224,7 @@ print_ip6_config (NMIPConfig *cfg6,
/* domains */
domain_arr = g_strdupv ((char **) nm_ip_config_get_domains (cfg6));
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX);
arr = nmc_dup_fields_array (tmpl, NMC_OF_FLAG_SECTION_PREFIX);
set_val_strc (arr, 0, group_prefix);
set_val_arr (arr, 1, addr_arr);
set_val_strc (arr, 2, nm_ip_config_get_gateway (cfg6));
......@@ -259,8 +246,8 @@ print_dhcp4_config (NMDhcpConfig *dhcp4,
const char *one_field)
{
GHashTable *table;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
const NMMetaAbstractInfo *const*tmpl;
NmcOutputField *arr;
if (dhcp4 == NULL)
return FALSE;
......@@ -273,11 +260,10 @@ print_dhcp4_config (NMDhcpConfig *dhcp4,
int i = 0;
NMC_OUTPUT_DATA_DEFINE_SCOPED (out);
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,
tmpl = (const NMMetaAbstractInfo *const*) nmc_fields_dhcp4_config;
out_indices = parse_output_fields (one_field,
tmpl, FALSE, NULL, NULL);
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES);
arr = nmc_dup_fields_array (tmpl, NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (out.output_data, arr);
options_arr = g_new (char *, g_hash_table_size (table) + 1);
......@@ -286,7 +272,7 @@ print_dhcp4_config (NMDhcpConfig *dhcp4,
options_arr[i++] = g_strdup_printf ("%s = %s", (char *) key, (char *) value);
options_arr[i] = NULL;
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX);
arr = nmc_dup_fields_array (tmpl, NMC_OF_FLAG_SECTION_PREFIX);
set_val_strc (arr, 0, group_prefix);
set_val_arr (arr, 1, options_arr);
g_ptr_array_add (out.output_data, arr);
......@@ -306,8 +292,8 @@ print_dhcp6_config (NMDhcpConfig *dhcp6,
const char *one_field)
{
GHashTable *table;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
const NMMetaAbstractInfo *const*tmpl;
NmcOutputField *arr;
if (dhcp6 == NULL)
return FALSE;
......@@ -320,11 +306,10 @@ print_dhcp6_config (NMDhcpConfig *dhcp6,
int i = 0;
NMC_OUTPUT_DATA_DEFINE_SCOPED (out);
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,
tmpl = (const NMMetaAbstractInfo *const*) nmc_fields_dhcp6_config;
out_indices = parse_output_fields (one_field,
tmpl, FALSE, NULL, NULL);
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES);
arr = nmc_dup_fields_array (tmpl, NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (out.output_data, arr);
options_arr = g_new (char *, g_hash_table_size (table) + 1);
......@@ -333,7 +318,7 @@ print_dhcp6_config (NMDhcpConfig *dhcp6,
options_arr[i++] = g_strdup_printf ("%s = %s", (char *) key, (char *) value);
options_arr[i] = NULL;
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX);
arr = nmc_dup_fields_array (tmpl, NMC_OF_FLAG_SECTION_PREFIX);
set_val_strc (arr, 0, group_prefix);
set_val_arr (arr, 1, options_arr);
g_ptr_array_add (out.output_data, arr);
......
......@@ -80,9 +80,9 @@ void nmc_complete_bool (const char *prefix);
const char *nmc_error_get_simple_message (GError *error);
extern NmcOutputField nmc_fields_ip4_config[];
extern NmcOutputField nmc_fields_dhcp4_config[];
extern NmcOutputField nmc_fields_ip6_config[];
extern NmcOutputField nmc_fields_dhcp6_config[];
extern const NmcMetaGenericInfo *const nmc_fields_ip4_config[];
extern const NmcMetaGenericInfo *const nmc_fields_dhcp4_config[];
extern const NmcMetaGenericInfo *const nmc_fields_ip6_config[];
extern const NmcMetaGenericInfo *const nmc_fields_dhcp6_config[];
#endif /* NMC_COMMON_H */
This diff is collapsed.
......@@ -35,10 +35,9 @@ nmc_read_connection_properties (NmCli *nmc,
void nmc_active_connection_state_to_color (NMActiveConnectionState state, NmcTermColor *color);
extern NmcOutputField nmc_fields_con_show[];
extern NmcOutputField nmc_fields_settings_names[];
extern NmcOutputField nmc_fields_con_active_details_general[];
extern NmcOutputField nmc_fields_con_active_details_vpn[];
extern NmcOutputField nmc_fields_con_active_details_groups[];
extern const NmcMetaGenericInfo *const nmc_fields_con_show[];
extern const NmcMetaGenericInfo *const nmc_fields_con_active_details_general[];
extern const NmcMetaGenericInfo *const nmc_fields_con_active_details_vpn[];
extern const NmcMetaGenericInfo *const nmc_fields_con_active_details_groups[];
#endif /* NMC_CONNECTIONS_H */
This diff is collapsed.
......@@ -34,20 +34,20 @@ NMDevice ** nmc_get_devices_sorted (NMClient *client);
void nmc_device_state_to_color (NMDeviceState state, NmcTermColor *color, NmcTermFormat *color_fmt);
extern NmcOutputField nmc_fields_dev_status[];
extern NmcOutputField nmc_fields_dev_show_general[];
extern NmcOutputField nmc_fields_dev_show_connections[];
extern NmcOutputField nmc_fields_dev_show_cap[];
extern NmcOutputField nmc_fields_dev_show_wired_prop[];
extern NmcOutputField nmc_fields_dev_show_wifi_prop[];
extern NmcOutputField nmc_fields_dev_show_wimax_prop[];
extern NmcOutputField nmc_fields_dev_wifi_list[];
extern NmcOutputField nmc_fields_dev_wimax_list[];
extern NmcOutputField nmc_fields_dev_show_master_prop[];
extern NmcOutputField nmc_fields_dev_show_team_prop[];
extern NmcOutputField nmc_fields_dev_show_vlan_prop[];
extern NmcOutputField nmc_fields_dev_show_bluetooth[];
extern NmcOutputField nmc_fields_dev_show_sections[];
extern NmcOutputField nmc_fields_dev_lldp_list[];
extern const NmcMetaGenericInfo *const nmc_fields_dev_status[];
extern const NmcMetaGenericInfo *const nmc_fields_dev_show_general[];
extern const NmcMetaGenericInfo *const nmc_fields_dev_show_connections[];
extern const NmcMetaGenericInfo *const nmc_fields_dev_show_cap[];
extern const NmcMetaGenericInfo *const nmc_fields_dev_show_wired_prop[];
extern const NmcMetaGenericInfo *const nmc_fields_dev_show_wifi_prop[];
extern const NmcMetaGenericInfo *const nmc_fields_dev_show_wimax_prop[];
extern const NmcMetaGenericInfo *const nmc_fields_dev_wifi_list[];
extern const NmcMetaGenericInfo *const nmc_fields_dev_wimax_list[];
extern const NmcMetaGenericInfo *const nmc_fields_dev_show_master_prop[];
extern const NmcMetaGenericInfo *const nmc_fields_dev_show_team_prop[];
extern const NmcMetaGenericInfo *const nmc_fields_dev_show_vlan_prop[];
extern const NmcMetaGenericInfo *const nmc_fields_dev_show_bluetooth[];
extern const NmcMetaGenericInfo *const nmc_fields_dev_show_sections[];
extern const NmcMetaGenericInfo *const nmc_fields_dev_lldp_list[];
#endif /* NMC_DEVICES_H */
......@@ -34,23 +34,20 @@
#include "devices.h"
#include "connections.h"
#define OUTPUT_FIELD_WITH_NAME(n) { .name = N_ (n), }
/* Available fields for 'general status' */
static NmcOutputField nmc_fields_nm_status[] = {
OUTPUT_FIELD_WITH_NAME ("RUNNING"), /* 0 */
OUTPUT_FIELD_WITH_NAME ("VERSION"), /* 1 */
OUTPUT_FIELD_WITH_NAME ("STATE"), /* 2 */
OUTPUT_FIELD_WITH_NAME ("STARTUP"), /* 3 */
OUTPUT_FIELD_WITH_NAME ("CONNECTIVITY"), /* 4 */
OUTPUT_FIELD_WITH_NAME ("NETWORKING"), /* 5 */
OUTPUT_FIELD_WITH_NAME ("WIFI-HW"), /* 6 */
OUTPUT_FIELD_WITH_NAME ("WIFI"), /* 7 */
OUTPUT_FIELD_WITH_NAME ("WWAN-HW"), /* 8 */
OUTPUT_FIELD_WITH_NAME ("WWAN"), /* 9 */
OUTPUT_FIELD_WITH_NAME ("WIMAX-HW"), /* 10 */
OUTPUT_FIELD_WITH_NAME ("WIMAX"), /* 11 */
{ 0 }
static const NmcMetaGenericInfo *const nmc_fields_nm_status[] = {
NMC_META_GENERIC ("RUNNING"), /* 0 */
NMC_META_GENERIC ("VERSION"), /* 1 */
NMC_META_GENERIC ("STATE"), /* 2 */
NMC_META_GENERIC ("STARTUP"), /* 3 */
NMC_META_GENERIC ("CONNECTIVITY"), /* 4 */
NMC_META_GENERIC ("NETWORKING"), /* 5 */
NMC_META_GENERIC ("WIFI-HW"), /* 6 */
NMC_META_GENERIC ("WIFI"), /* 7 */
NMC_META_GENERIC ("WWAN-HW"), /* 8 */
NMC_META_GENERIC ("WWAN"), /* 9 */
NMC_META_GENERIC ("WIMAX-HW"), /* 10 */
NMC_META_GENERIC ("WIMAX"), /* 11 */
NULL,
};
#define NMC_FIELDS_NM_STATUS_ALL "RUNNING,VERSION,STATE,STARTUP,CONNECTIVITY,NETWORKING,WIFI-HW,WIFI,WWAN-HW,WWAN"
#define NMC_FIELDS_NM_STATUS_SWITCH "NETWORKING,WIFI-HW,WIFI,WWAN-HW,WWAN"
......@@ -64,19 +61,19 @@ static NmcOutputField nmc_fields_nm_status[] = {
/* Available fields for 'general permissions' */
static NmcOutputField nmc_fields_nm_permissions[] = {
OUTPUT_FIELD_WITH_NAME ("PERMISSION"), /* 0 */
OUTPUT_FIELD_WITH_NAME ("VALUE"), /* 1 */
{ 0 }
static const NmcMetaGenericInfo *const nmc_fields_nm_permissions[] = {
NMC_META_GENERIC ("PERMISSION"), /* 0 */
NMC_META_GENERIC ("VALUE"), /* 1 */
NULL,
};
#define NMC_FIELDS_NM_PERMISSIONS_ALL "PERMISSION,VALUE"
#define NMC_FIELDS_NM_PERMISSIONS_COMMON "PERMISSION,VALUE"
/* Available fields for 'general logging' */
static NmcOutputField nmc_fields_nm_logging[] = {
OUTPUT_FIELD_WITH_NAME ("LEVEL"), /* 0 */
OUTPUT_FIELD_WITH_NAME ("DOMAINS"), /* 1 */
{ 0 }
static const NmcMetaGenericInfo *const nmc_fields_nm_logging[] = {
NMC_META_GENERIC ("LEVEL"), /* 0 */
NMC_META_GENERIC ("DOMAINS"), /* 1 */
NULL,
};
#define NMC_FIELDS_NM_LOGGING_ALL "LEVEL,DOMAINS"
#define NMC_FIELDS_NM_LOGGING_COMMON "LEVEL,DOMAINS"
......@@ -323,8 +320,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;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
const NMMetaAbstractInfo *const*tmpl;
NmcOutputField *arr;
NMC_OUTPUT_DATA_DEFINE_SCOPED (out);
if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0)
......@@ -334,8 +331,7 @@ show_nm_status (NmCli *nmc, const char *pretty_header_name, const char *print_fl
else
fields_str = nmc->required_fields;
tmpl = nmc_fields_nm_status;
tmpl_len = sizeof (nmc_fields_nm_status);
tmpl = (const NMMetaAbstractInfo *const*) nmc_fields_nm_status;
out_indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error);
if (error) {
......@@ -354,10 +350,10 @@ 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);
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES);
arr = nmc_dup_fields_array (tmpl, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (out.output_data, arr);
arr = nmc_dup_fields_array (tmpl, tmpl_len, 0);
arr = nmc_dup_fields_array (tmpl, 0);
set_val_strc (arr, 0, _("running"));
set_val_strc (arr, 1, nm_client_get_version (nmc->client));
set_val_strc (arr, 2, nm_state_to_string (state));
......@@ -474,8 +470,8 @@ print_permissions (void *user_data)
const char *fields_str;
const char *fields_all = NMC_FIELDS_NM_PERMISSIONS_ALL;
const char *fields_common = NMC_FIELDS_NM_PERMISSIONS_COMMON;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
const NMMetaAbstractInfo *const*tmpl;
NmcOutputField *arr;
NMC_OUTPUT_DATA_DEFINE_SCOPED (out);
if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0)
......@@ -485,8 +481,7 @@ print_permissions (void *user_data)
else
fields_str = nmc->required_fields;
tmpl = nmc_fields_nm_permissions;
tmpl_len = sizeof (nmc_fields_nm_permissions);
tmpl = (const NMMetaAbstractInfo *const*) nmc_fields_nm_permissions;
out_indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error);
if (error) {
......@@ -496,14 +491,14 @@ print_permissions (void *user_data)
return FALSE;
}
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES);
arr = nmc_dup_fields_array (tmpl, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (out.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);
arr = nmc_dup_fields_array (tmpl, tmpl_len, 0);
arr = nmc_dup_fields_array (tmpl, 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 (out.output_data, arr);
......@@ -588,8 +583,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;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
const NMMetaAbstractInfo *const*tmpl;
NmcOutputField *arr;
NMC_OUTPUT_DATA_DEFINE_SCOPED (out);
if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0)
......@@ -599,8 +594,7 @@ show_general_logging (NmCli *nmc)
else
fields_str = nmc->required_fields;
tmpl = nmc_fields_nm_logging;
tmpl_len = sizeof (nmc_fields_nm_logging);
tmpl = (const NMMetaAbstractInfo *const*) nmc_fields_nm_logging;
out_indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error);
if (error) {
......@@ -618,10 +612,10 @@ show_general_logging (NmCli *nmc)
return FALSE;
}
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES);
arr = nmc_dup_fields_array (tmpl, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (out.output_data, arr);
arr = nmc_dup_fields_array (tmpl, tmpl_len, 0);
arr = nmc_dup_fields_array (tmpl, 0);
set_val_str (arr, 0, level);
set_val_str (arr, 1, domains);
g_ptr_array_add (out.output_data, arr);
......
......@@ -51,10 +51,23 @@
#endif
/* Global NmCli object */
// FIXME: Currently, we pass NmCli over in most APIs, but we might refactor
// that and use the global variable directly instead.
NmCli nm_cli;
/*****************************************************************************/
static const char *
_meta_type_nmc_generic_info_get_name (const NMMetaAbstractInfo *abstract_info)
{
return ((const NmcMetaGenericInfo *) abstract_info)->name;
}
const NMMetaType nmc_meta_type_generic_info = {
.type_name = "nmc-generic-info",
.get_name = _meta_type_nmc_generic_info_get_name,
};
/*****************************************************************************/
typedef struct {
NmCli *nmc;
int argc;
......@@ -83,16 +96,12 @@ complete_field_setting (GHashTable *h, NMMetaSettingType setting_type)
}
static void
complete_field (GHashTable *h, const char *setting, const NmcOutputField *field)
complete_field (GHashTable *h, const NmcMetaGenericInfo *const*field)
{
int i;
for (i = 0; field[i].name; i++) {
if (setting)
g_hash_table_add (h, g_strdup_printf ("%s.%s", setting, field[i].name));
else
g_hash_table_add (h, g_strdup (field[i].name));
}
for (i = 0; field[i]; i++)
g_hash_table_add (h, g_strdup (field[i]->name));
}
static void
......@@ -122,30 +131,29 @@ complete_fields (const char *prefix)
h = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
complete_field (h, NULL, nmc_fields_ip4_config);
complete_field (h, NULL, nmc_fields_dhcp4_config);
complete_field (h, NULL, nmc_fields_ip6_config);
complete_field (h, NULL, nmc_fields_dhcp6_config);
complete_field (h, NULL, nmc_fields_con_show);
complete_field (h, NULL, nmc_fields_settings_names);
complete_field (h, NULL, nmc_fields_con_active_details_general);
complete_field (h, NULL, nmc_fields_con_active_details_vpn);
complete_field (h, NULL, nmc_fields_con_active_details_groups);
complete_field (h, NULL, nmc_fields_dev_status);
complete_field (h, NULL, nmc_fields_dev_show_general);
complete_field (h, NULL, nmc_fields_dev_show_connections);
complete_field (h, NULL, nmc_fields_dev_show_cap);
complete_field (h, NULL, nmc_fields_dev_show_wired_prop);
complete_field (h, NULL, nmc_fields_dev_show_wifi_prop);
complete_field (h, NULL, nmc_fields_dev_show_wimax_prop);
complete_field (h, NULL, nmc_fields_dev_wifi_list);
complete_field (h, NULL, nmc_fields_dev_wimax_list);
complete_field (h, NULL, nmc_fields_dev_show_master_prop);
complete_field (h, NULL, nmc_fields_dev_show_team_prop);
complete_field (h, NULL, nmc_fields_dev_show_vlan_prop);
complete_field (h, NULL, nmc_fields_dev_show_bluetooth);
complete_field (h, NULL, nmc_fields_dev_show_sections);
complete_field (h, NULL, nmc_fields_dev_lldp_list);
complete_field (h, nmc_fields_ip4_config);
complete_field (h, nmc_fields_dhcp4_config);
complete_field (h, nmc_fields_ip6_config);
complete_field (h, nmc_fields_dhcp6_config);
complete_field (h, nmc_fields_con_show);
complete_field (h, nmc_fields_con_active_details_general);
complete_field (h, nmc_fields_con_active_details_vpn);
complete_field (h, nmc_fields_con_active_details_groups);
complete_field (h, nmc_fields_dev_status);
complete_field (h, nmc_fields_dev_show_general);
complete_field (h, nmc_fields_dev_show_connections);
complete_field (h, nmc_fields_dev_show_cap);
complete_field (h, nmc_fields_dev_show_wired_prop);
complete_field (h, nmc_fields_dev_show_wifi_prop);
complete_field (h, nmc_fields_dev_show_wimax_prop);
complete_field (h, nmc_fields_dev_wifi_list);
complete_field (h, nmc_fields_dev_wimax_list);
complete_field (h, nmc_fields_dev_show_master_prop);
complete_field (h, nmc_fields_dev_show_team_prop);
complete_field (h, nmc_fields_dev_show_vlan_prop);
complete_field (h, nmc_fields_dev_show_bluetooth);
complete_field (h, nmc_fields_dev_show_sections);
complete_field (h, nmc_fields_dev_lldp_list);
for (i = 0; i < _NM_META_SETTING_TYPE_NUM; i++)
complete_field_setting (h, i);
......
......@@ -23,6 +23,8 @@
#include "NetworkManager.h"
#include "nm-secret-agent-old.h"
#include "nm-meta-setting-desc.h"
#if WITH_POLKIT_AGENT
#include "nm-polkit-listener.h"
#else
......@@ -106,27 +108,37 @@ typedef enum {
NMC_OF_FLAG_MAIN_HEADER_ONLY = 0x00000008, /* Print main header only */
} NmcOfFlags;
struct _NMMetaSettingInfoEditor;
extern const const NMMetaType nmc_meta_type_generic_info;
typedef struct _NmcOutputField NmcOutputField;
typedef struct _NmcMetaGenericInfo NmcMetaGenericInfo;
struct _NmcMetaGenericInfo {
const NMMetaType *meta_type;
const char *name;
const NmcMetaGenericInfo *const*nested;
};
typedef struct _NmcOutputField {
const char *name; /* Field's name */
#define NMC_META_GENERIC(n, ...) \
(&((NmcMetaGenericInfo) { \
.meta_type = &nmc_meta_type_generic_info, \
.name = N_ (n), \
__VA_ARGS__ \
}))
#define NMC_META_GENERIC_WITH_NESTED(n, nest) \
NMC_META_GENERIC (n, .nested = (nest))
struct _NmcOutputField {
const NMMetaAbstractInfo *info;
int width; /* Width in screen columns */
const struct _NmcOutputField *group_list; /* Points to an array with available section field names if this is a section (group) field */
void *value; /* Value of current field - char* or char** (NULL-terminated array) */
gboolean value_is_array; /* Whether value is char** instead of char* */
gboolean free_value; /* Whether to free the value */
NmcOfFlags flags; /* Flags - whether and how to print values/field names/headers */
NmcTermColor color; /* Use this color to print value */
NmcTermFormat color_fmt; /* Use this terminal format to print value */
/* in a very particular case NmcOutputField is used in combination with
* the @group_list above. That list will go away (and the entire NmcOutputField
* should separate formatting-options, setting-metadata and output.
*
* For now, hack around that by alternatively providing a @setting_info instead
* of @group_list. */
const struct _NMMetaSettingInfoEditor *setting_info;
} NmcOutputField;
};
typedef enum {
NMC_USE_COLOR_AUTO,
......
......@@ -35,32 +35,6 @@
/*****************************************************************************/
static const NmcOutputField *
_get_nmc_output_fields (const NMMetaSettingInfoEditor *setting_info)
{
static NmcOutputField *fields[_NM_META_SETTING_TYPE_NUM + 1] = { };
NmcOutputField **field;
guint i;
g_return_val_if_fail (setting_info, NULL);
g_return_val_if_fail (setting_info->general->meta_type < _NM_META_SETTING_TYPE_NUM, NULL);
field = &fields[setting_info->general->meta_type];
if (G_UNLIKELY (!*field)) {
*field = g_new0 (NmcOutputField, setting_info->properties_num + 1);
for (i = 0; i < setting_info->properties_num; i++) {
NmcOutputField *f = &(*field)[i];
f->name = setting_info->properties[i].property_name;
}
}
return *field;
}
/*****************************************************************************/
static gboolean
get_answer (const char *prop, const char *value)
{
......@@ -833,40 +807,33 @@ nmc_property_set_gvalue (NMSetting *setting, const char *prop, GValue *value)
/*****************************************************************************/
static char *
_all_properties (const NMMetaSettingInfoEditor *setting_info)
static NmcOutputField *
_dup_fields_array (const NMMetaSettingInfoEditor *setting_info, NmcOfFlags flags)
{
GString *str;
guint i;
NmcOutputField *row;
gsize l;
str = g_string_sized_new (250);
for (i = 0; i < setting_info->properties_num; i++) {
if (str->len)
g_string_append_c (str, ',');
g_string_append (str, setting_info->properties[i].property_name);
}
return g_string_free (str, FALSE);
l = setting_info->properties_num;
row = g_malloc0 ((l + 1) * sizeof (NmcOutputField));
for (l = 0; l < setting_info->properties_num; l++)
row[l].info = (const NMMetaAbstractInfo *) &setting_info->properties[l];
row[0].flags = flags;
return row;
}
gboolean
setting_details (NMSetting *setting, NmCli *nmc, const char *one_prop, gboolean show_secrets)
{
const NMMetaSettingInfo *meta_setting_info;
const NMMetaSettingInfoEditor *setting_info;
gs_free NmcOutputField *tmpl = NULL;
NmcOutputField *arr;
guint i;
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);
meta_setting_info = nm_meta_setting_infos_by_gtype (G_OBJECT_TYPE (setting));
g_return_val_if_fail (meta_setting_info, FALSE);
setting_info = &nm_meta_setting_infos_editor[meta_setting_info->meta_type];
setting_info = nm_meta_setting_info_editor_find_by_setting (setting);
g_return_val_if_fail (setting_info, FALSE);
g_return_val_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (setting, setting_info->general->get_setting_gtype ()), FALSE);
......@@ -874,15 +841,13 @@ setting_details (NMSetting *setting, NmCli *nmc, const char *one_prop, gboolean
if (nmc->nmc_config.print_output == NMC_PRINT_TERSE)
type = NM_META_ACCESSOR_GET_TYPE_PARSABLE;
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)),
tmpl, FALSE, NULL, NULL);
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES);
out_indices = parse_output_fields (one_prop,
(const NMMetaAbstractInfo *const*) nm_property_infos_for_setting_type (setting_info->general->meta_type),
FALSE, NULL, NULL);
arr = _dup_fields_array (setting_info, NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (out.output_data, arr);
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX);
arr = _dup_fields_array (setting_info, NMC_OF_FLAG_SECTION_PREFIX);
for (i = 0; i < setting_info->properties_num; i++) {
const NMMetaPropertyInfo *property_info = &setting_info->properties[i];
......
This diff is collapsed.
......@@ -61,12 +61,12 @@ void set_val_color_all (NmcOutputField fields_array[], NmcTermColor color);
void set_val_color_fmt_all (NmcOutputField fields_array[], NmcTermFormat format);
void nmc_free_output_field_values (NmcOutputField fields_array[]);
GArray *parse_output_fields (const char *fields_str,
const NmcOutputField fields_array[],