cli: merge branch 'th/cli-meta-3'

This commit is contained in:
Thomas Haller 2017-04-13 10:10:56 +02:00
commit ecec03c8b7
10 changed files with 1175 additions and 1451 deletions

View file

@ -1314,8 +1314,8 @@ split_required_fields_for_con_show (const char *input,
else if (!strcasecmp (*iter, CON_SHOW_DETAIL_GROUP_ACTIVE))
group_active = TRUE;
else {
char *allowed1 = nmc_get_allowed_fields ((const NMMetaAbstractInfo *const*) nm_meta_setting_infos_editor_p (), NULL);
char *allowed2 = nmc_get_allowed_fields ((const NMMetaAbstractInfo *const*) nmc_fields_con_active_details_groups, NULL);
char *allowed1 = nm_meta_abstract_infos_get_names_str ((const NMMetaAbstractInfo *const*) nm_meta_setting_infos_editor_p (), NULL);
char *allowed2 = nm_meta_abstract_infos_get_names_str ((const NMMetaAbstractInfo *const*) nmc_fields_con_active_details_groups, NULL);
g_set_error (error, NMCLI_ERROR, 0, _("invalid field '%s'; allowed fields: %s and %s, or %s,%s"),
*iter, allowed1, allowed2, CON_SHOW_DETAIL_GROUP_PROFILE, CON_SHOW_DETAIL_GROUP_ACTIVE);
g_free (allowed1);
@ -2907,7 +2907,7 @@ get_valid_properties_string (const NMMetaSettingValidPartItem *const*array,
gchar *new;
const char *arg_name;
arg_name = setting_info->properties[j].property_name;
arg_name = setting_info->properties[j]->property_name;
/* If required, expand the alias too */
if (!postfix && setting_info->alias) {
@ -3319,7 +3319,7 @@ _meta_property_needs_bond_hack (const NMMetaPropertyInfo *property_info)
if (!property_info)
g_return_val_if_reached (FALSE);
return property_info->property_typ_data
&& property_info->property_typ_data->subtype.nested.data == &nm_meta_property_typ_data_bond;
&& property_info->property_typ_data->nested == &nm_meta_property_typ_data_bond;
}
@ -3353,28 +3353,16 @@ _meta_abstract_get (const NMMetaAbstractInfo *abstract_info,
const char **out_prompt,
const char **out_def_hint)
{
/* _meta_property_needs_bond_hack () */
if (abstract_info->meta_type == &nm_meta_type_nested_property_info) {
const NMMetaNestedPropertyTypeInfo *info = (const NMMetaNestedPropertyTypeInfo *) abstract_info;
const NMMetaPropertyInfo *info = (const NMMetaPropertyInfo *) abstract_info;
NM_SET_OUT (out_setting_info, info->parent_info->setting_info);
NM_SET_OUT (out_setting_name, info->parent_info->setting_info->general->setting_name);
NM_SET_OUT (out_property_name, info->parent_info->property_name);
NM_SET_OUT (out_option, info->field_name);
NM_SET_OUT (out_inf_flags, info->inf_flags);
NM_SET_OUT (out_prompt, info->prompt);
NM_SET_OUT (out_def_hint, info->def_hint);
} else {
const NMMetaPropertyInfo *info = (const NMMetaPropertyInfo *) abstract_info;
NM_SET_OUT (out_setting_info, info->setting_info);
NM_SET_OUT (out_setting_name, info->setting_info->general->setting_name);
NM_SET_OUT (out_property_name, info->property_name);
NM_SET_OUT (out_option, info->property_alias);
NM_SET_OUT (out_inf_flags, info->inf_flags);
NM_SET_OUT (out_prompt, info->prompt);
NM_SET_OUT (out_def_hint, info->def_hint);
}
NM_SET_OUT (out_option, info->property_alias);
NM_SET_OUT (out_setting_info, info->setting_info);
NM_SET_OUT (out_setting_name, info->setting_info->general->setting_name);
NM_SET_OUT (out_property_name, info->property_name);
NM_SET_OUT (out_option, info->property_alias);
NM_SET_OUT (out_inf_flags, info->inf_flags);
NM_SET_OUT (out_prompt, info->prompt);
NM_SET_OUT (out_def_hint, info->def_hint);
}
static const OptionInfo *_meta_abstract_get_option_info (const NMMetaAbstractInfo *abstract_info);
@ -3397,11 +3385,11 @@ enable_options (const gchar *setting_name, const gchar *property, const gchar *
guint i;
for (i = 0; i < nm_meta_property_typ_data_bond.nested_len; i++) {
const NMMetaNestedPropertyTypeInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
const NMMetaNestedPropertyInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
if ( bi->inf_flags & NM_META_PROPERTY_INF_FLAG_DONT_ASK
&& bi->field_name
&& g_strv_contains (opts, bi->field_name))
if ( bi->base.inf_flags & NM_META_PROPERTY_INF_FLAG_DONT_ASK
&& bi->base.property_alias
&& g_strv_contains (opts, bi->base.property_alias))
_dynamic_options_set ((const NMMetaAbstractInfo *) bi, PROPERTY_INF_FLAG_ENABLED, PROPERTY_INF_FLAG_ENABLED);
}
return;
@ -3446,7 +3434,9 @@ disable_options (const gchar *setting_name, const gchar *property)
setting_info = nm_meta_setting_info_editor_find_by_name (setting_name, FALSE);
if (!setting_info)
g_return_if_reached ();
property_infos = nm_property_infos_for_setting_type (setting_info->general->meta_type);
property_infos = setting_info->properties;
if (!property_infos)
return;
}
for (p = 0; property_infos[p]; p++) {
@ -3456,7 +3446,7 @@ disable_options (const gchar *setting_name, const gchar *property)
guint i;
for (i = 0; i < nm_meta_property_typ_data_bond.nested_len; i++) {
const NMMetaNestedPropertyTypeInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
const NMMetaNestedPropertyInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
_dynamic_options_set ((const NMMetaAbstractInfo *) bi, PROPERTY_INF_FLAG_DISABLED, PROPERTY_INF_FLAG_DISABLED);
}
@ -3482,7 +3472,9 @@ reset_options (void)
const NMMetaPropertyInfo *const*property_infos;
guint p;
property_infos = nm_property_infos_for_setting_type (s);
property_infos = nm_meta_setting_infos_editor[s].properties;
if (!property_infos)
continue;
for (p = 0; property_infos[p]; p++) {
const NMMetaPropertyInfo *property_info = property_infos[p];
@ -3490,7 +3482,7 @@ reset_options (void)
guint i;
for (i = 0; i < nm_meta_property_typ_data_bond.nested_len; i++) {
const NMMetaNestedPropertyTypeInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
const NMMetaNestedPropertyInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
_dynamic_options_set ((const NMMetaAbstractInfo *) bi, PROPERTY_INF_FLAG_ALL, 0);
}
@ -4062,7 +4054,9 @@ complete_property_name (NmCli *nmc, NMConnection *connection,
if (!nm_connection_get_setting_by_name (connection, nm_meta_setting_infos_editor[s].general->setting_name))
continue;
property_infos = nm_property_infos_for_setting_type (s);
property_infos = nm_meta_setting_infos_editor[s].properties;
if (!property_infos)
continue;
for (p = 0; property_infos[p]; p++) {
const NMMetaPropertyInfo *property_info = property_infos[p];
@ -4070,12 +4064,12 @@ complete_property_name (NmCli *nmc, NMConnection *connection,
guint i;
for (i = 0; i < nm_meta_property_typ_data_bond.nested_len; i++) {
const NMMetaNestedPropertyTypeInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
const NMMetaNestedPropertyInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
if ( !bi->field_name
|| !g_str_has_prefix (bi->field_name, prefix))
if ( !bi->base.property_alias
|| !g_str_has_prefix (bi->base.property_alias, prefix))
continue;
g_print ("%s\n", bi->field_name);
g_print ("%s\n", bi->base.property_alias);
}
} else {
if (!property_info->is_cli_option)
@ -4256,7 +4250,9 @@ nmc_read_connection_properties (NmCli *nmc,
type_settings, slv_settings, NULL))
continue;
property_infos = nm_property_infos_for_setting_type (s);
property_infos = nm_meta_setting_infos_editor[s].properties;
if (!property_infos)
continue;
for (p = 0; property_infos[p]; p++) {
const NMMetaPropertyInfo *property_info = property_infos[p];
@ -4264,9 +4260,9 @@ nmc_read_connection_properties (NmCli *nmc,
guint i;
for (i = 0; i < nm_meta_property_typ_data_bond.nested_len; i++) {
const NMMetaNestedPropertyTypeInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
const NMMetaNestedPropertyInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
if (!nm_streq0 (bi->field_name, option))
if (!nm_streq0 (bi->base.property_alias, option))
continue;
if (chosen) {
g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT,
@ -4420,7 +4416,9 @@ nmcli_con_add_tab_completion (const char *text, int start, int end)
const NMMetaPropertyInfo *const*property_infos;
guint p;
property_infos = nm_property_infos_for_setting_type (s);
property_infos = nm_meta_setting_infos_editor[s].properties;
if (!property_infos)
continue;
for (p = 0; property_infos[p]; p++) {
const NMMetaPropertyInfo *property_info = property_infos[p];
@ -4428,10 +4426,10 @@ nmcli_con_add_tab_completion (const char *text, int start, int end)
guint i;
for (i = 0; i < nm_meta_property_typ_data_bond.nested_len; i++) {
const NMMetaNestedPropertyTypeInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
const NMMetaNestedPropertyInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
if ( bi->prompt
&& g_str_has_prefix (rl_prompt, bi->prompt)) {
if ( bi->base.prompt
&& g_str_has_prefix (rl_prompt, bi->base.prompt)) {
goto next;
}
}
@ -4513,7 +4511,9 @@ questionnaire_mandatory (NmCli *nmc, NMConnection *connection)
const NMMetaPropertyInfo *const*property_infos;
guint p;
property_infos = nm_property_infos_for_setting_type (s);
property_infos = nm_meta_setting_infos_editor[s].properties;
if (!property_infos)
continue;
for (p = 0; property_infos[p]; p++) {
const NMMetaPropertyInfo *property_info = property_infos[p];
@ -4521,11 +4521,11 @@ questionnaire_mandatory (NmCli *nmc, NMConnection *connection)
guint i;
for (i = 0; i < nm_meta_property_typ_data_bond.nested_len; i++) {
const NMMetaNestedPropertyTypeInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
const NMMetaNestedPropertyInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
if (!option_relevant (connection, (const NMMetaAbstractInfo *) bi))
continue;
if ( (bi->inf_flags & NM_META_PROPERTY_INF_FLAG_REQD)
if ( (bi->base.inf_flags & NM_META_PROPERTY_INF_FLAG_REQD)
|| (_dynamic_options_get ((const NMMetaAbstractInfo *) bi) & PROPERTY_INF_FLAG_ENABLED))
ask_option (nmc, connection, (const NMMetaAbstractInfo *) bi);
}
@ -4584,13 +4584,15 @@ again:
&& s != s_asking)
continue;
property_infos = nm_property_infos_for_setting_type (s);
property_infos = nm_meta_setting_infos_editor[s].properties;
if (!property_infos)
continue;
for (p = 0; property_infos[p]; p++) {
const NMMetaPropertyInfo *property_info = property_infos[p];
if (_meta_property_needs_bond_hack (property_info)) {
for (i = 0; i < nm_meta_property_typ_data_bond.nested_len; i++) {
const NMMetaNestedPropertyTypeInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
const NMMetaNestedPropertyInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
if (!option_relevant (connection, (const NMMetaAbstractInfo *) bi))
continue;
@ -4755,7 +4757,9 @@ read_properties:
const NMMetaPropertyInfo *const*property_infos;
guint p;
property_infos = nm_property_infos_for_setting_type (s);
property_infos = nm_meta_setting_infos_editor[s].properties;
if (!property_infos)
continue;
for (p = 0; property_infos[p]; p++) {
const NMMetaPropertyInfo *property_info = property_infos[p];
@ -4763,12 +4767,12 @@ read_properties:
guint i;
for (i = 0; i < nm_meta_property_typ_data_bond.nested_len; i++) {
const NMMetaNestedPropertyTypeInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
const NMMetaNestedPropertyInfo *bi = &nm_meta_property_typ_data_bond.nested[i];
if (!option_relevant (connection, (const NMMetaAbstractInfo *) bi))
continue;
if (bi->inf_flags & NM_META_PROPERTY_INF_FLAG_REQD) {
g_string_printf (nmc->return_text, _("Error: '%s' argument is required."), bi->field_name);
if (bi->base.inf_flags & NM_META_PROPERTY_INF_FLAG_REQD) {
g_string_printf (nmc->return_text, _("Error: '%s' argument is required."), bi->base.property_alias);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
goto finish;
}

View file

@ -77,7 +77,7 @@ complete_field_setting (GHashTable *h, NMMetaSettingType setting_type)
for (i = 0; i < setting_info->properties_num; i++) {
g_hash_table_add (h, g_strdup_printf ("%s.%s",
setting_info->general->setting_name,
setting_info->properties[i].property_name));
setting_info->properties[i]->property_name));
}
}

View file

@ -56,19 +56,11 @@ _meta_type_nmc_generic_info_get_nested (const NMMetaAbstractInfo *abstract_info,
gpointer *out_to_free)
{
const NmcMetaGenericInfo *info;
guint n;
info = (const NmcMetaGenericInfo *) abstract_info;
if (out_len) {
n = 0;
if (info->nested) {
for (; info->nested[n]; n++) {
}
}
*out_len = n;
}
*out_to_free = NULL;
NM_SET_OUT (out_len, NM_PTRARRAY_LEN (info->nested));
return (const NMMetaAbstractInfo *const*) info->nested;
}
@ -764,224 +756,10 @@ nmc_free_output_field_values (NmcOutputField fields_array[])
/*****************************************************************************/
typedef struct {
guint idx;
gsize self_offset_plus_1;
gsize sub_offset_plus_1;
} OutputSelectionItem;
static NmcOutputSelection *
_output_selection_pack (const NMMetaAbstractInfo *const* fields_array,
GArray *array,
GString *str)
{
NmcOutputSelection *result;
guint i;
guint len;
len = array ? array->len : 0;
/* re-organize the collected output data in one buffer that can be freed using
* g_free(). This makes allocation more complicated, but saves us from special
* handling for free. */
result = g_malloc0 (sizeof (NmcOutputSelection) + (len * sizeof (NmcOutputSelectionItem)) + (str ? str->len : 0));
*((guint *) &result->num) = len;
if (len > 0) {
char *pdata = &((char *) result)[sizeof (NmcOutputSelection) + (len * sizeof (NmcOutputSelectionItem))];
if (str)
memcpy (pdata, str->str, str->len);
for (i = 0; i < len; i++) {
const OutputSelectionItem *a = &g_array_index (array, OutputSelectionItem, i);
NmcOutputSelectionItem *p = (NmcOutputSelectionItem *) &result->items[i];
p->info = fields_array[a->idx];
p->idx = a->idx;
if (a->self_offset_plus_1 > 0)
p->self_selection = &pdata[a->self_offset_plus_1 - 1];
if (a->sub_offset_plus_1 > 0)
p->sub_selection = &pdata[a->sub_offset_plus_1 - 1];
}
}
return result;
}
static gboolean
_output_selection_select_one (const NMMetaAbstractInfo *const* fields_array,
const char *fields_prefix,
const char *fields_str,
gboolean validate_nested,
GArray **p_array,
GString **p_str,
GError **error)
{
guint i, j;
const char *i_name;
const char *right;
gboolean found = FALSE;
const NMMetaAbstractInfo *fields_array_failure = NULL;
gs_free char *fields_str_clone = NULL;
nm_assert (fields_str);
nm_assert (p_array);
nm_assert (p_str);
nm_assert (!error || !*error);
right = strchr (fields_str, '.');
if (right) {
fields_str_clone = g_strdup (fields_str);
fields_str_clone[right - fields_str] = '\0';
i_name = fields_str_clone;
right = &fields_str_clone[right - fields_str + 1];
} else
i_name = fields_str;
if (!fields_array)
goto not_found;
for (i = 0; fields_array[i]; i++) {
const NMMetaAbstractInfo *fi = fields_array[i];
if (g_ascii_strcasecmp (i_name, nm_meta_abstract_info_get_name (fi, FALSE)) != 0)
continue;
if (!right || !validate_nested) {
found = TRUE;
break;
}
if (fi->meta_type == &nm_meta_type_setting_info_editor) {
const NMMetaSettingInfoEditor *fi_s = (const NMMetaSettingInfoEditor *) fi;
for (j = 0; j < fi_s->properties_num; j++) {
if (g_ascii_strcasecmp (right, fi_s->properties[j].property_name) == 0) {
found = TRUE;
break;
}
}
} else if (fi->meta_type == &nmc_meta_type_generic_info) {
const NmcMetaGenericInfo *fi_g = (const NmcMetaGenericInfo *) fi;
for (j = 0; fi_g->nested && fi_g->nested[j]; j++) {
if (g_ascii_strcasecmp (right, nm_meta_abstract_info_get_name ((const NMMetaAbstractInfo *) fi_g->nested[j], FALSE)) == 0) {
found = TRUE;
break;
}
}
}
fields_array_failure = fields_array[i];
break;
}
if (!found) {
not_found:
if ( !right
&& !fields_prefix
&& ( !g_ascii_strcasecmp (i_name, "all")
|| !g_ascii_strcasecmp (i_name, "common")))
g_set_error (error, NMCLI_ERROR, 0, _("field '%s' has to be alone"), i_name);
else {
gs_free char *allowed_fields = NULL;
if (fields_array_failure) {
gs_free char *p = NULL;
if (fields_prefix) {
p = g_strdup_printf ("%s.%s", fields_prefix,
nm_meta_abstract_info_get_name (fields_array_failure, FALSE));
}
allowed_fields = nmc_get_allowed_fields_nested (fields_array_failure, p);
} else
allowed_fields = nmc_get_allowed_fields (fields_array, NULL);
g_set_error (error, NMCLI_ERROR, 1, _("invalid field '%s%s%s%s%s'; %s%s%s"),
fields_prefix ?: "", fields_prefix ? "." : "",
i_name, right ? "." : "", right ?: "",
NM_PRINT_FMT_QUOTED (allowed_fields, "allowed fields: ", allowed_fields, "", "no fields"));
}
return FALSE;
}
{
GString *str;
OutputSelectionItem s = {
.idx = i,
};
if (!*p_str)
*p_str = g_string_sized_new (64);
str = *p_str;
s.self_offset_plus_1 = str->len + 1;
if (fields_prefix) {
g_string_append (str, fields_prefix);
g_string_append_c (str, '.');
}
g_string_append_len (str, i_name, strlen (i_name) + 1);
if (right) {
s.sub_offset_plus_1 = str->len + 1;
g_string_append_len (str, right, strlen (right) + 1);
}
if (!*p_array)
*p_array = g_array_new (FALSE, FALSE, sizeof (OutputSelectionItem));
g_array_append_val (*p_array, s);
}
return TRUE;
}
static NmcOutputSelection *
_output_selection_create_all (const NMMetaAbstractInfo *const* fields_array)
{
gs_unref_array GArray *array = NULL;
guint i;
if (fields_array) {
array = g_array_new (FALSE, FALSE, sizeof (OutputSelectionItem));
for (i = 0; fields_array[i]; i++) {
OutputSelectionItem s = {
.idx = i,
};
g_array_append_val (array, s);
}
}
return _output_selection_pack (fields_array, array, NULL);
}
static NmcOutputSelection *
_output_selection_create_one (const NMMetaAbstractInfo *const* fields_array,
const char *fields_prefix,
const char *fields_str, /* one field selector (contains not commas) and is alrady stripped of spaces. */
gboolean validate_nested,
GError **error)
{
gs_unref_array GArray *array = NULL;
nm_auto_free_gstring GString *str = NULL;
g_return_val_if_fail (!error || !*error, NULL);
nm_assert (fields_str && !strchr (fields_str, ','));
if (!_output_selection_select_one (fields_array,
fields_prefix,
fields_str,
validate_nested,
&array,
&str,
error))
return NULL;
return _output_selection_pack (fields_array, array, str);
}
#define PRINT_DATA_COL_PARENT_NIL (G_MAXUINT)
typedef struct {
const NmcOutputSelectionItem *selection_item;
const NMMetaSelectionItem *selection_item;
guint parent_idx;
guint self_idx;
bool is_leaf;
@ -991,7 +769,7 @@ static gboolean
_output_selection_append (GArray *cols,
const char *fields_prefix,
guint parent_idx,
const NmcOutputSelectionItem *selection_item,
const NMMetaSelectionItem *selection_item,
GPtrArray *gfree_keeper,
GError **error)
{
@ -999,8 +777,8 @@ _output_selection_append (GArray *cols,
guint col_idx;
guint i;
const NMMetaAbstractInfo *const*nested;
NmcOutputSelection *selection;
const NmcOutputSelectionItem *si;
NMMetaSelectionResultList *selection;
const NMMetaSelectionItem *si;
col_idx = cols->len;
@ -1022,7 +800,7 @@ _output_selection_append (GArray *cols,
if (parent_idx != PRINT_DATA_COL_PARENT_NIL) {
si = g_array_index (cols, PrintDataCol, parent_idx).selection_item;
allowed_fields = nmc_get_allowed_fields_nested (si->info, si->self_selection);
allowed_fields = nm_meta_abstract_info_get_nested_names_str (si->info, si->self_selection);
}
if (!allowed_fields) {
g_set_error (error, NMCLI_ERROR, 1, _("invalid field '%s%s%s'; no such field"),
@ -1037,13 +815,13 @@ _output_selection_append (GArray *cols,
return FALSE;
}
selection = _output_selection_create_one (nested, selection_item->self_selection,
selection_item->sub_selection, FALSE, error);
selection = nm_meta_selection_create_parse_one (nested, selection_item->self_selection,
selection_item->sub_selection, FALSE, error);
if (!selection)
return FALSE;
nm_assert (selection->num == 1);
} else if (nested) {
selection = _output_selection_create_all (nested);
selection = nm_meta_selection_create_all (nested);
nm_assert (selection && selection->num > 0);
} else
selection = NULL;
@ -1069,47 +847,6 @@ _output_selection_append (GArray *cols,
/*****************************************************************************/
NmcOutputSelection *
nmc_output_selection_create (const NMMetaAbstractInfo *const* fields_array,
const char *fields_prefix,
const char *fields_str, /* a comma separated list of selectors */
gboolean validate_nested,
GError **error)
{
gs_unref_array GArray *array = NULL;
nm_auto_free_gstring GString *str = NULL;
gs_free char *fields_str_clone = NULL;
char *fields_str_cur;
char *fields_str_next;
g_return_val_if_fail (!error || !*error, NULL);
if (!fields_str)
return _output_selection_create_all (fields_array);
fields_str_clone = g_strdup (fields_str);
for (fields_str_cur = fields_str_clone; fields_str_cur; fields_str_cur = fields_str_next) {
fields_str_cur = nm_str_skip_leading_spaces (fields_str_cur);
fields_str_next = strchr (fields_str_cur, ',');
if (fields_str_next)
*fields_str_next++ = '\0';
g_strchomp (fields_str_cur);
if (!fields_str_cur[0])
continue;
if (!_output_selection_select_one (fields_array,
fields_prefix,
fields_str_cur,
validate_nested,
&array,
&str,
error))
return NULL;
}
return _output_selection_pack (fields_array, array, str);
}
/**
* _output_selection_parse:
* @fields: a %NULL terminated array of meta-data fields
@ -1133,12 +870,12 @@ _output_selection_parse (const NMMetaAbstractInfo *const*fields,
GPtrArray **out_gfree_keeper,
GError **error)
{
NmcOutputSelection *selection;
NMMetaSelectionResultList *selection;
gs_unref_ptrarray GPtrArray *gfree_keeper = NULL;
gs_unref_array GArray *cols = NULL;
guint i;
selection = nmc_output_selection_create (fields, NULL, fields_str, FALSE, error);
selection = nm_meta_selection_create_parse_list (fields, NULL, fields_str, FALSE, error);
if (!selection)
return FALSE;
@ -1153,7 +890,7 @@ _output_selection_parse (const NMMetaAbstractInfo *const*fields,
cols = g_array_new (FALSE, TRUE, sizeof (PrintDataCol));
for (i = 0; i < selection->num; i++) {
const NmcOutputSelectionItem *si = &selection->items[i];
const NMMetaSelectionItem *si = &selection->items[i];
if (!_output_selection_append (cols, NULL, PRINT_DATA_COL_PARENT_NIL,
si, gfree_keeper, error))
@ -1194,7 +931,7 @@ parse_output_fields (const char *fields_str,
GPtrArray **out_group_fields,
GError **error)
{
gs_free NmcOutputSelection *selection = NULL;
gs_free NMMetaSelectionResultList *selection = NULL;
GArray *array;
GPtrArray *group_fields = NULL;
guint i;
@ -1202,7 +939,7 @@ parse_output_fields (const char *fields_str,
g_return_val_if_fail (!error || !*error, NULL);
g_return_val_if_fail (!out_group_fields || !*out_group_fields, NULL);
selection = nmc_output_selection_create (fields_array, NULL, fields_str, TRUE, error);
selection = nm_meta_selection_create_parse_list (fields_array, NULL, fields_str, TRUE, error);
if (!selection)
return NULL;
@ -1223,50 +960,6 @@ parse_output_fields (const char *fields_str,
return array;
}
char *
nmc_get_allowed_fields_nested (const NMMetaAbstractInfo *abstract_info, const char *name_prefix)
{
gs_free gpointer nested_to_free = NULL;
guint i;
const NMMetaAbstractInfo *const*nested;
GString *allowed_fields;
nested = nm_meta_abstract_info_get_nested (abstract_info, NULL, &nested_to_free);
if (!nested)
return NULL;
allowed_fields = g_string_sized_new (256);
if (!name_prefix)
name_prefix = nm_meta_abstract_info_get_name (abstract_info, FALSE);
for (i = 0; nested[i]; i++) {
g_string_append_printf (allowed_fields, "%s.%s,",
name_prefix, nm_meta_abstract_info_get_name (nested[i], FALSE));
}
g_string_truncate (allowed_fields, allowed_fields->len - 1);
return g_string_free (allowed_fields, FALSE);
}
char *
nmc_get_allowed_fields (const NMMetaAbstractInfo *const*fields_array, const char *name_prefix)
{
GString *allowed_fields;
guint i;
if (!fields_array || !fields_array[0])
return NULL;
allowed_fields = g_string_sized_new (256);
for (i = 0; fields_array[i]; i++) {
if (name_prefix)
g_string_append_printf (allowed_fields, "%s.", name_prefix);
g_string_append_printf (allowed_fields, "%s,", nm_meta_abstract_info_get_name (fields_array[i], FALSE));
}
g_string_truncate (allowed_fields, allowed_fields->len - 1);
return g_string_free (allowed_fields, FALSE);
}
NmcOutputField *
nmc_dup_fields_array (const NMMetaAbstractInfo *const*fields, NmcOfFlags flags)
{
@ -1539,7 +1232,7 @@ static gboolean
_print_skip_column (const NmcConfig *nmc_config,
const PrintDataHeaderCell *header_cell)
{
const NmcOutputSelectionItem *selection_item;
const NMMetaSelectionItem *selection_item;
const NMMetaAbstractInfo *info;
selection_item = header_cell->col->selection_item;

View file

@ -61,31 +61,11 @@ void set_val_color_all (NmcOutputField fields_array[], NMMetaTermColor color);
void set_val_color_fmt_all (NmcOutputField fields_array[], NMMetaTermFormat format);
void nmc_free_output_field_values (NmcOutputField fields_array[]);
typedef struct {
const NMMetaAbstractInfo *info;
const char *self_selection;
const char *sub_selection;
guint idx;
} NmcOutputSelectionItem;
typedef struct {
const guint num;
const NmcOutputSelectionItem items[];
} NmcOutputSelection;
NmcOutputSelection *nmc_output_selection_create (const NMMetaAbstractInfo *const* fields_array,
const char *fields_prefix,
const char *fields_str,
gboolean validate_nested,
GError **error);
GArray *parse_output_fields (const char *fields_str,
const NMMetaAbstractInfo *const* fields_array,
gboolean parse_groups,
GPtrArray **group_fields,
GError **error);
char *nmc_get_allowed_fields_nested (const NMMetaAbstractInfo *abstract_info, const char *name_prefix);
char *nmc_get_allowed_fields (const NMMetaAbstractInfo *const*fields_array, const char *name_prefix);
NmcOutputField *nmc_dup_fields_array (const NMMetaAbstractInfo *const*fields, NmcOfFlags flags);
void nmc_empty_output_fields (NmcOutputData *output_data);
void print_required_fields (const NmcConfig *nmc_config,

View file

@ -101,10 +101,10 @@ nm_meta_setting_info_editor_get_property_info (const NMMetaSettingInfoEditor *se
g_return_val_if_fail (property_name, NULL);
for (i = 0; i < setting_info->properties_num; i++) {
nm_assert (setting_info->properties[i].property_name);
nm_assert (setting_info->properties[i].setting_info == setting_info);
if (nm_streq (setting_info->properties[i].property_name, property_name))
return &setting_info->properties[i];
nm_assert (setting_info->properties[i]->property_name);
nm_assert (setting_info->properties[i]->setting_info == setting_info);
if (nm_streq (setting_info->properties[i]->property_name, property_name))
return setting_info->properties[i];
}
return NULL;
@ -170,31 +170,6 @@ nm_meta_setting_info_editor_new_setting (const NMMetaSettingInfoEditor *setting_
/*****************************************************************************/
/* this basically returns NMMetaSettingType.properties, but with type
* (NMMetaPropertyInfo **) instead of (NMMetaPropertyInfo *), which is
* required by some APIs. */
const NMMetaPropertyInfo *const*
nm_property_infos_for_setting_type (NMMetaSettingType setting_type)
{
static const NMMetaPropertyInfo **cache[_NM_META_SETTING_TYPE_NUM] = { NULL };
const NMMetaPropertyInfo **p;
guint i;
nm_assert (setting_type < _NM_META_SETTING_TYPE_NUM);
nm_assert (setting_type == 0 || setting_type > 0);
if (G_UNLIKELY (!(p = cache[setting_type]))) {
const NMMetaSettingInfoEditor *setting_info = &nm_meta_setting_infos_editor[setting_type];
p = g_new (const NMMetaPropertyInfo *, setting_info->properties_num + 1);
for (i = 0; i < setting_info->properties_num; i++)
p[i] = &setting_info->properties[i];
p[i] = NULL;
cache[setting_type] = p;
}
return (const NMMetaPropertyInfo *const*) p;
}
const NMMetaSettingInfoEditor *const*
nm_meta_setting_infos_editor_p (void)
{
@ -349,3 +324,298 @@ nm_meta_abstract_info_complete (const NMMetaAbstractInfo *abstract_info,
return (const char *const*) (*out_to_free = r);
}
}
/*****************************************************************************/
char *
nm_meta_abstract_info_get_nested_names_str (const NMMetaAbstractInfo *abstract_info, const char *name_prefix)
{
gs_free gpointer nested_to_free = NULL;
guint i;
const NMMetaAbstractInfo *const*nested;
GString *allowed_fields;
nested = nm_meta_abstract_info_get_nested (abstract_info, NULL, &nested_to_free);
if (!nested)
return NULL;
allowed_fields = g_string_sized_new (256);
if (!name_prefix)
name_prefix = nm_meta_abstract_info_get_name (abstract_info, FALSE);
for (i = 0; nested[i]; i++) {
g_string_append_printf (allowed_fields, "%s.%s,",
name_prefix, nm_meta_abstract_info_get_name (nested[i], FALSE));
}
g_string_truncate (allowed_fields, allowed_fields->len - 1);
return g_string_free (allowed_fields, FALSE);
}
char *
nm_meta_abstract_infos_get_names_str (const NMMetaAbstractInfo *const*fields_array, const char *name_prefix)
{
GString *allowed_fields;
guint i;
if (!fields_array || !fields_array[0])
return NULL;
allowed_fields = g_string_sized_new (256);
for (i = 0; fields_array[i]; i++) {
if (name_prefix)
g_string_append_printf (allowed_fields, "%s.", name_prefix);
g_string_append_printf (allowed_fields, "%s,", nm_meta_abstract_info_get_name (fields_array[i], FALSE));
}
g_string_truncate (allowed_fields, allowed_fields->len - 1);
return g_string_free (allowed_fields, FALSE);
}
/*****************************************************************************/
typedef struct {
guint idx;
gsize self_offset_plus_1;
gsize sub_offset_plus_1;
} OutputSelectionItem;
static NMMetaSelectionResultList *
_output_selection_pack (const NMMetaAbstractInfo *const* fields_array,
GArray *array,
GString *str)
{
NMMetaSelectionResultList *result;
guint i;
guint len;
len = array ? array->len : 0;
/* re-organize the collected output data in one buffer that can be freed using
* g_free(). This makes allocation more complicated, but saves us from special
* handling for free. */
result = g_malloc0 (sizeof (NMMetaSelectionResultList) + (len * sizeof (NMMetaSelectionItem)) + (str ? str->len : 0));
*((guint *) &result->num) = len;
if (len > 0) {
char *pdata = &((char *) result)[sizeof (NMMetaSelectionResultList) + (len * sizeof (NMMetaSelectionItem))];
if (str)
memcpy (pdata, str->str, str->len);
for (i = 0; i < len; i++) {
const OutputSelectionItem *a = &g_array_index (array, OutputSelectionItem, i);
NMMetaSelectionItem *p = (NMMetaSelectionItem *) &result->items[i];
p->info = fields_array[a->idx];
p->idx = a->idx;
if (a->self_offset_plus_1 > 0)
p->self_selection = &pdata[a->self_offset_plus_1 - 1];
if (a->sub_offset_plus_1 > 0)
p->sub_selection = &pdata[a->sub_offset_plus_1 - 1];
}
}
return result;
}
static gboolean
_output_selection_select_one (const NMMetaAbstractInfo *const* fields_array,
const char *fields_prefix,
const char *fields_str,
gboolean validate_nested,
GArray **p_array,
GString **p_str,
GError **error)
{
guint i, j;
const char *i_name;
const char *right;
gboolean found = FALSE;
const NMMetaAbstractInfo *fields_array_failure = NULL;
gs_free char *fields_str_clone = NULL;
nm_assert (fields_str);
nm_assert (p_array);
nm_assert (p_str);
nm_assert (!error || !*error);
right = strchr (fields_str, '.');
if (right) {
fields_str_clone = g_strdup (fields_str);
fields_str_clone[right - fields_str] = '\0';
i_name = fields_str_clone;
right = &fields_str_clone[right - fields_str + 1];
} else
i_name = fields_str;
if (!fields_array)
goto not_found;
for (i = 0; fields_array[i]; i++) {
const NMMetaAbstractInfo *fi = fields_array[i];
const NMMetaAbstractInfo *const*nested;
gs_free gpointer nested_to_free = NULL;
if (g_ascii_strcasecmp (i_name, nm_meta_abstract_info_get_name (fi, FALSE)) != 0)
continue;
if (!right || !validate_nested) {
found = TRUE;
break;
}
nested = nm_meta_abstract_info_get_nested (fi, NULL, &nested_to_free);
if (nested) {
for (j = 0; nested[j]; nested++) {
if (g_ascii_strcasecmp (right, nm_meta_abstract_info_get_name (nested[j], FALSE)) == 0) {
found = TRUE;
break;
}
}
}
fields_array_failure = fields_array[i];
break;
}
if (!found) {
not_found:
if ( !right
&& !fields_prefix
&& ( !g_ascii_strcasecmp (i_name, "all")
|| !g_ascii_strcasecmp (i_name, "common")))
g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN, _("field '%s' has to be alone"), i_name);
else {
gs_free char *allowed_fields = NULL;
if (fields_array_failure) {
gs_free char *p = NULL;
if (fields_prefix) {
p = g_strdup_printf ("%s.%s", fields_prefix,
nm_meta_abstract_info_get_name (fields_array_failure, FALSE));
}
allowed_fields = nm_meta_abstract_info_get_nested_names_str (fields_array_failure, p);
} else
allowed_fields = nm_meta_abstract_infos_get_names_str (fields_array, NULL);
g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN, _("invalid field '%s%s%s%s%s'; %s%s%s"),
fields_prefix ?: "", fields_prefix ? "." : "",
i_name, right ? "." : "", right ?: "",
NM_PRINT_FMT_QUOTED (allowed_fields, "allowed fields: ", allowed_fields, "", "no fields"));
}
return FALSE;
}
{
GString *str;
OutputSelectionItem s = {
.idx = i,
};
if (!*p_str)
*p_str = g_string_sized_new (64);
str = *p_str;
s.self_offset_plus_1 = str->len + 1;
if (fields_prefix) {
g_string_append (str, fields_prefix);
g_string_append_c (str, '.');
}
g_string_append_len (str, i_name, strlen (i_name) + 1);
if (right) {
s.sub_offset_plus_1 = str->len + 1;
g_string_append_len (str, right, strlen (right) + 1);
}
if (!*p_array)
*p_array = g_array_new (FALSE, FALSE, sizeof (OutputSelectionItem));
g_array_append_val (*p_array, s);
}
return TRUE;
}
NMMetaSelectionResultList *
nm_meta_selection_create_all (const NMMetaAbstractInfo *const* fields_array)
{
gs_unref_array GArray *array = NULL;
guint i;
if (fields_array) {
array = g_array_new (FALSE, FALSE, sizeof (OutputSelectionItem));
for (i = 0; fields_array[i]; i++) {
OutputSelectionItem s = {
.idx = i,
};
g_array_append_val (array, s);
}
}
return _output_selection_pack (fields_array, array, NULL);
}
NMMetaSelectionResultList *
nm_meta_selection_create_parse_one (const NMMetaAbstractInfo *const* fields_array,
const char *fields_prefix,
const char *fields_str, /* one field selector (contains no commas) and is already stripped of spaces. */
gboolean validate_nested,
GError **error)
{
gs_unref_array GArray *array = NULL;
nm_auto_free_gstring GString *str = NULL;
g_return_val_if_fail (!error || !*error, NULL);
nm_assert (fields_str && !strchr (fields_str, ','));
if (!_output_selection_select_one (fields_array,
fields_prefix,
fields_str,
validate_nested,
&array,
&str,
error))
return NULL;
return _output_selection_pack (fields_array, array, str);
}
NMMetaSelectionResultList *
nm_meta_selection_create_parse_list (const NMMetaAbstractInfo *const* fields_array,
const char *fields_prefix,
const char *fields_str, /* a comma separated list of selectors */
gboolean validate_nested,
GError **error)
{
gs_unref_array GArray *array = NULL;
nm_auto_free_gstring GString *str = NULL;
gs_free char *fields_str_clone = NULL;
char *fields_str_cur;
char *fields_str_next;
g_return_val_if_fail (!error || !*error, NULL);
if (!fields_str)
return nm_meta_selection_create_all (fields_array);
fields_str_clone = g_strdup (fields_str);
for (fields_str_cur = fields_str_clone; fields_str_cur; fields_str_cur = fields_str_next) {
fields_str_cur = nm_str_skip_leading_spaces (fields_str_cur);
fields_str_next = strchr (fields_str_cur, ',');
if (fields_str_next)
*fields_str_next++ = '\0';
g_strchomp (fields_str_cur);
if (!fields_str_cur[0])
continue;
if (!_output_selection_select_one (fields_array,
fields_prefix,
fields_str_cur,
validate_nested,
&array,
&str,
error))
return NULL;
}
return _output_selection_pack (fields_array, array, str);
}

View file

@ -41,8 +41,6 @@ const NMMetaPropertyInfo *nm_meta_property_info_find_by_setting (NMSetting *sett
/*****************************************************************************/
const NMMetaPropertyInfo *const*nm_property_infos_for_setting_type (NMMetaSettingType setting_type);
const NMMetaSettingInfoEditor *const*nm_meta_setting_infos_editor_p (void);
/*****************************************************************************/
@ -71,4 +69,33 @@ const char *const*nm_meta_abstract_info_complete (const NMMetaAbstractInfo *abst
/*****************************************************************************/
char *nm_meta_abstract_info_get_nested_names_str (const NMMetaAbstractInfo *abstract_info, const char *name_prefix);
char *nm_meta_abstract_infos_get_names_str (const NMMetaAbstractInfo *const*fields_array, const char *name_prefix);
/*****************************************************************************/
typedef struct {
const NMMetaAbstractInfo *info;
const char *self_selection;
const char *sub_selection;
guint idx;
} NMMetaSelectionItem;
typedef struct {
const guint num;
const NMMetaSelectionItem items[];
} NMMetaSelectionResultList;
NMMetaSelectionResultList *nm_meta_selection_create_all (const NMMetaAbstractInfo *const* fields_array);
NMMetaSelectionResultList *nm_meta_selection_create_parse_one (const NMMetaAbstractInfo *const* fields_array,
const char *fields_prefix,
const char *fields_str,
gboolean validate_nested,
GError **error);
NMMetaSelectionResultList *nm_meta_selection_create_parse_list (const NMMetaAbstractInfo *const* fields_array,
const char *fields_prefix,
const char *fields_str,
gboolean validate_nested,
GError **error);
#endif /* _NM_META_SETTING_ACCESS_H__ */

File diff suppressed because it is too large Load diff

View file

@ -174,6 +174,8 @@ typedef struct _NMMetaPropertyInfo NMMetaPropertyInfo;
typedef struct _NMMetaPropertyType NMMetaPropertyType;
typedef struct _NMMetaPropertyTypData NMMetaPropertyTypData;
typedef struct _NMMetaOperationContext NMMetaOperationContext;
typedef struct _NMMetaNestedPropertyInfo NMMetaNestedPropertyInfo;
typedef struct _NMMetaPropertyTypDataNested NMMetaPropertyTypDataNested;
/* this gives some context information for virtual functions.
* This command actually violates layering, and should be considered
@ -221,8 +223,6 @@ struct _NMMetaPropertyType {
struct _NMUtilsEnumValueInfo;
struct _NMMetaPropertyTypDataNested;
struct _NMMetaPropertyTypData {
union {
struct {
@ -240,11 +240,9 @@ struct _NMMetaPropertyTypData {
struct {
NMMetaPropertyTypeMacMode mode;
} mac;
struct {
const struct _NMMetaPropertyTypDataNested *data;
} nested;
} subtype;
const char *const*values_static;
const NMMetaPropertyTypDataNested *nested;
NMMetaPropertyTypFlags typ_flags;
};
@ -260,8 +258,8 @@ enum {
_NM_META_PROPERTY_TYPE_CONNECTION_TYPE = 4,
};
#define nm_meta_property_info_connection_type (&nm_meta_setting_infos_editor[NM_META_SETTING_TYPE_CONNECTION].properties[_NM_META_PROPERTY_TYPE_CONNECTION_TYPE])
#define nm_meta_property_info_vpn_service_type (&nm_meta_setting_infos_editor[NM_META_SETTING_TYPE_VPN].properties[_NM_META_PROPERTY_TYPE_VPN_SERVICE_TYPE])
#define nm_meta_property_info_connection_type (nm_meta_setting_infos_editor[NM_META_SETTING_TYPE_CONNECTION].properties[_NM_META_PROPERTY_TYPE_CONNECTION_TYPE])
#define nm_meta_property_info_vpn_service_type (nm_meta_setting_infos_editor[NM_META_SETTING_TYPE_VPN].properties[_NM_META_PROPERTY_TYPE_VPN_SERVICE_TYPE])
struct _NMMetaPropertyInfo {
const NMMetaType *meta_type;
@ -299,9 +297,7 @@ struct _NMMetaSettingInfoEditor {
const NMMetaSettingInfo *general;
const char *alias;
const char *pretty_name;
/* the order of the properties matter. The first *must* be the
* "name", and then the order is as they are listed by default. */
const NMMetaPropertyInfo *properties;
const NMMetaPropertyInfo *const*properties;
guint properties_num;
/* a NMConnection has a main type (connection.type), which is a
@ -394,19 +390,18 @@ struct _NMMetaEnvironment {
extern const NMMetaType nm_meta_type_nested_property_info;
typedef struct _NMMetaNestedPropertyTypeInfo {
const NMMetaType *meta_type;
struct _NMMetaNestedPropertyInfo {
union {
const NMMetaType *meta_type;
NMMetaPropertyInfo base;
};
const NMMetaPropertyInfo *parent_info;
const char *field_name;
NMMetaPropertyInfFlags inf_flags;
const char *prompt;
const char *def_hint;
} NMMetaNestedPropertyTypeInfo;
};
typedef struct _NMMetaPropertyTypDataNested {
const NMMetaNestedPropertyTypeInfo *nested;
struct _NMMetaPropertyTypDataNested {
const NMMetaNestedPropertyInfo *nested;
guint nested_len;
} NMMetaPropertyTypDataNested;
};
const NMMetaPropertyTypDataNested nm_meta_property_typ_data_bond;

View file

@ -74,13 +74,16 @@ test_client_meta_check (void)
g_assert_cmpstr (info->general->setting_name, ==, info->meta_type->get_name ((const NMMetaAbstractInfo *) info, FALSE));
g_assert_cmpstr ("name", ==, info->meta_type->get_name ((const NMMetaAbstractInfo *) info, TRUE));
g_assert (info->properties_num == NM_PTRARRAY_LEN (info->properties));
if (info->properties_num) {
gs_unref_hashtable GHashTable *property_names = g_hash_table_new (g_str_hash, g_str_equal);
g_assert (info->properties);
for (p = 0; p < info->properties_num; p++) {
const NMMetaPropertyInfo *pi = &info->properties[p];
const NMMetaPropertyInfo *pi = info->properties[p];
g_assert (pi);
g_assert (pi->meta_type == &nm_meta_type_property_info);
g_assert (pi->setting_info == info);
g_assert (pi->property_name);
@ -93,6 +96,7 @@ test_client_meta_check (void)
g_assert (pi->property_type);
g_assert (pi->property_type->get_fcn);
}
g_assert (!info->properties[info->properties_num]);
} else
g_assert (!info->properties);
@ -120,18 +124,6 @@ test_client_meta_check (void)
}
}
for (m = 0; m < _NM_META_SETTING_TYPE_NUM; m++) {
const NMMetaPropertyInfo *const*pis;
const NMMetaSettingInfoEditor *info = &nm_meta_setting_infos_editor[m];
pis = nm_property_infos_for_setting_type (m);
g_assert (pis);
for (p = 0; p < info->properties_num; p++)
g_assert (pis[p] == &info->properties[p]);
g_assert (!pis[p]);
}
for (m = 0; m < _NM_META_SETTING_TYPE_NUM; m++) {
const NMMetaSettingInfoEditor *info = &nm_meta_setting_infos_editor[m];
@ -139,7 +131,7 @@ test_client_meta_check (void)
g_assert (nm_meta_setting_info_editor_find_by_gtype (info->general->get_setting_gtype ()) == info);
for (p = 0; p < info->properties_num; p++) {
const NMMetaPropertyInfo *pi = &info->properties[p];
const NMMetaPropertyInfo *pi = info->properties[p];
g_assert (nm_meta_setting_info_editor_get_property_info (info, pi->property_name) == pi);
g_assert (nm_meta_property_info_find_by_name (info->general->setting_name, pi->property_name) == pi);

View file

@ -11,6 +11,7 @@ clients/cli/polkit-agent.c
clients/cli/settings.c
clients/cli/utils.c
clients/common/nm-client-utils.c
clients/common/nm-meta-setting-access.c
clients/common/nm-meta-setting-desc.c
clients/common/nm-polkit-listener.c
clients/common/nm-secret-agent-simple.c