diff options
author | Beniamino Galvani <bgalvani@redhat.com> | 2017-04-20 14:35:07 +0200 |
---|---|---|
committer | Beniamino Galvani <bgalvani@redhat.com> | 2017-04-20 14:35:07 +0200 |
commit | 104795317f8e560b9b3549e4f03185f24ba473a3 (patch) | |
tree | 1a9978ecb7429625efeacb167badc99cd38ac0cf | |
parent | 212df5cbd873dba67a6b24135dc45170a40959a5 (diff) | |
parent | ed824102ff17a243114f985b0ef8874fb465f1c0 (diff) | |
download | NetworkManager-104795317f8e560b9b3549e4f03185f24ba473a3.tar.gz |
merge: branch 'bg/cli-completion'
Fix autocompletion and other regressions in nmcli editor.
-rw-r--r-- | clients/cli/common.c | 9 | ||||
-rw-r--r-- | clients/cli/connections.c | 113 |
2 files changed, 74 insertions, 48 deletions
diff --git a/clients/cli/common.c b/clients/cli/common.c index e37d9d8c8b..c9e0bbbd43 100644 --- a/clients/cli/common.c +++ b/clients/cli/common.c @@ -1137,8 +1137,13 @@ _rl_compentry_func_wrap_fcn (const char *text, int state) { g_return_val_if_fail (_rl_compentry_func_wrap.initialized, NULL); - if ( !_rl_compentry_func_wrap.values - || !_rl_compentry_func_wrap.values[_rl_compentry_func_wrap.idx]) { + while ( _rl_compentry_func_wrap.values + && _rl_compentry_func_wrap.values[_rl_compentry_func_wrap.idx] + && !g_str_has_prefix (_rl_compentry_func_wrap.values[_rl_compentry_func_wrap.idx], text)) + _rl_compentry_func_wrap.idx++; + + if ( !_rl_compentry_func_wrap.values + || !_rl_compentry_func_wrap.values[_rl_compentry_func_wrap.idx]) { g_strfreev (_rl_compentry_func_wrap.values); _rl_compentry_func_wrap.values = NULL; _rl_compentry_func_wrap.initialized = FALSE; diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 6d679f7d99..fc738d5cb5 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -2822,7 +2822,7 @@ get_name_alias_toplevel (const char *name, const char *slave_type) * Returns: string; the caller is responsible for freeing it. */ static char * -Get_valid_options_string (const NMMetaSettingValidPartItem *const*array, const NMMetaSettingValidPartItem *const*array_slv) +get_valid_options_string (const NMMetaSettingValidPartItem *const*array, const NMMetaSettingValidPartItem *const*array_slv) { const NMMetaSettingValidPartItem *const*iter = array; GString *str; @@ -2855,6 +2855,9 @@ get_valid_options_string_toplevel (void) for (i = 0; i < _NM_META_SETTING_TYPE_NUM; i++) { const NMMetaSettingInfoEditor *setting_info = &nm_meta_setting_infos_editor[i]; + if (!setting_info->valid_parts) + continue; + if (str->len) g_string_append (str, ", "); if (setting_info->alias) @@ -2862,6 +2865,11 @@ get_valid_options_string_toplevel (void) else g_string_append (str, setting_info->general->setting_name); } + + if (str->len) + g_string_append (str, ", "); + g_string_append (str, "bond-slave, bridge-slave, team-slave"); + return g_string_free (str, FALSE); } @@ -3015,7 +3023,7 @@ check_valid_name (const char *val, const NMMetaSettingValidPartItem *const*array /* We want to handle aliases, so construct own error message */ gs_free char *err_str = NULL; - err_str = Get_valid_options_string (array, array_slv); + err_str = get_valid_options_string (array, array_slv); g_set_error (error, 1, 0, _("'%s' not among [%s]"), val, err_str); g_clear_error (&tmp_err); } @@ -3041,7 +3049,7 @@ check_valid_name (const char *val, const NMMetaSettingValidPartItem *const*array } static const char * -check_valid_name_toplevel (const char *val, GError **error) +check_valid_name_toplevel (const char *val, const char **slave_type, GError **error) { gs_unref_ptrarray GPtrArray *tmp_arr = NULL; const char *str; @@ -3049,6 +3057,8 @@ check_valid_name_toplevel (const char *val, GError **error) int i; const NMMetaSettingInfoEditor *setting_info; + NM_SET_OUT (slave_type, NULL); + /* Create a temporary array that can be used in nmc_string_is_valid() */ tmp_arr = g_ptr_array_sized_new (32); for (i = 0; i < _NM_META_SETTING_TYPE_NUM; i++) { @@ -3057,6 +3067,9 @@ check_valid_name_toplevel (const char *val, GError **error) if (setting_info->alias) g_ptr_array_add (tmp_arr, (gpointer) setting_info->alias); } + g_ptr_array_add (tmp_arr, "bond-slave"); + g_ptr_array_add (tmp_arr, "bridge-slave"); + g_ptr_array_add (tmp_arr, "team-slave"); g_ptr_array_add (tmp_arr, (gpointer) NULL); /* Check string validity */ @@ -3075,6 +3088,17 @@ check_valid_name_toplevel (const char *val, GError **error) return NULL; } + if (nm_streq (str, "bond-slave")) { + NM_SET_OUT (slave_type, NM_SETTING_BOND_SETTING_NAME); + return NM_SETTING_WIRED_SETTING_NAME; + } else if (nm_streq (str, "bridge-slave")) { + NM_SET_OUT (slave_type, NM_SETTING_BRIDGE_SETTING_NAME); + return NM_SETTING_WIRED_SETTING_NAME; + } else if (nm_streq (str, "team-slave")) { + NM_SET_OUT (slave_type, NM_SETTING_TEAM_SETTING_NAME); + return NM_SETTING_WIRED_SETTING_NAME; + } + setting_info = nm_meta_setting_info_editor_find_by_name (str, TRUE); if (setting_info) return setting_info->general->setting_name; @@ -3730,39 +3754,23 @@ set_connection_type (NmCli *nmc, NMConnection *con, const OptionInfo *option, co const NMMetaSettingValidPartItem *const*slv_settings; GError *local = NULL; const gchar *master[] = { "master", NULL }; + const char *slave_type = NULL; - if (g_strcmp0 (value, "bond-slave") == 0) { - value = NM_SETTING_WIRED_SETTING_NAME; - if (!set_property (con, NM_SETTING_CONNECTION_SETTING_NAME, - NM_SETTING_CONNECTION_SLAVE_TYPE, NM_SETTING_BOND_SETTING_NAME, - '\0', error)) { - return FALSE; - } - enable_options (NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_MASTER, master); - } else if (g_strcmp0 (value, "bridge-slave") == 0) { - value = NM_SETTING_WIRED_SETTING_NAME; - if (!set_property (con, NM_SETTING_CONNECTION_SETTING_NAME, - NM_SETTING_CONNECTION_SLAVE_TYPE, NM_SETTING_BRIDGE_SETTING_NAME, - '\0', error)) { - return FALSE; - } - enable_options (NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_MASTER, master); - } else if (g_strcmp0 (value, "team-slave") == 0) { - value = NM_SETTING_WIRED_SETTING_NAME; + value = check_valid_name_toplevel (value, &slave_type, &local); + if (!value) { + g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, + _("Error: bad connection type: %s"), local->message); + g_clear_error (&local); + return FALSE; + } + + if (slave_type) { if (!set_property (con, NM_SETTING_CONNECTION_SETTING_NAME, - NM_SETTING_CONNECTION_SLAVE_TYPE, NM_SETTING_TEAM_SETTING_NAME, + NM_SETTING_CONNECTION_SLAVE_TYPE, slave_type, '\0', error)) { return FALSE; } enable_options (NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_MASTER, master); - } else { - value = check_valid_name_toplevel (value, &local); - if (!value) { - g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("Error: bad connection type: %s"), local->message); - g_clear_error (&local); - return FALSE; - } } /* ifname is mandatory for all connection types except virtual ones (bond, team, bridge, vlan) */ @@ -4971,10 +4979,29 @@ gen_cmd_save (const char *text, int state) static rl_compentry_func_t * gen_connection_types (const char *text) { - gs_strfreev char **values = NULL; + gs_free char **values = NULL; + const NMMetaSettingInfoEditor *editor; + GPtrArray *array; + int i; + + array = g_ptr_array_new (); + + for (i = 0; i < _NM_META_SETTING_TYPE_NUM; i++) { + editor = &nm_meta_setting_infos_editor[i]; + if (!editor->valid_parts) + continue; + g_ptr_array_add (array, (gpointer) nm_meta_setting_infos[i].setting_name); + if (editor->alias) + g_ptr_array_add (array, (gpointer) editor->alias); + } + + g_ptr_array_add (array, "bond-slave"); + g_ptr_array_add (array, "bridge-slave"); + g_ptr_array_add (array, "team-slave"); + g_ptr_array_add (array, NULL); + + values = (char **) g_ptr_array_free (array, FALSE); - values = _meta_abstract_complete ((const NMMetaAbstractInfo *) nm_meta_property_info_connection_type, - text); return nmc_rl_compentry_func_wrap ((const char *const*) values); } @@ -6766,7 +6793,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t valid_settings_main = get_valid_settings_array (connection_type); valid_settings_slave = nm_meta_setting_info_valid_parts_for_slave_type (s_type, NULL); - valid_settings_str = Get_valid_options_string (valid_settings_main, valid_settings_slave); + valid_settings_str = get_valid_options_string (valid_settings_main, valid_settings_slave); g_print (_("You may edit the following settings: %s\n"), valid_settings_str); menu_ctx.level = 0; @@ -7531,12 +7558,11 @@ get_ethernet_device_name (NmCli *nmc) } static void -editor_init_new_connection (NmCli *nmc, NMConnection *connection) +editor_init_new_connection (NmCli *nmc, NMConnection *connection, const char *slave_type) { NMSetting *setting, *base_setting; NMSettingConnection *s_con; const char *con_type; - const char *slave_type = NULL; s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); @@ -7546,13 +7572,6 @@ editor_init_new_connection (NmCli *nmc, NMConnection *connection) nmc_setting_connection_connect_handlers (s_con, connection); - if (g_strcmp0 (con_type, "bond-slave") == 0) - slave_type = NM_SETTING_BOND_SETTING_NAME; - if (g_strcmp0 (con_type, "team-slave") == 0) - slave_type = NM_SETTING_TEAM_SETTING_NAME; - if (g_strcmp0 (con_type, "bridge-slave") == 0) - slave_type = NM_SETTING_BRIDGE_SETTING_NAME; - if (slave_type) { const char *dev_ifname = get_ethernet_device_name (nmc); @@ -7754,6 +7773,8 @@ do_connection_edit (NmCli *nmc, int argc, char **argv) editor_init_existing_connection (connection); } else { + const char *slave_type = NULL; + /* New connection */ if (nmc->complete) { if (type && argc == 0) @@ -7761,7 +7782,7 @@ do_connection_edit (NmCli *nmc, int argc, char **argv) goto error; } - connection_type = check_valid_name_toplevel (type, &err1); + connection_type = check_valid_name_toplevel (type, &slave_type, &err1); tmp_str = get_valid_options_string_toplevel (); while (!connection_type) { @@ -7773,7 +7794,7 @@ do_connection_edit (NmCli *nmc, int argc, char **argv) type_ask = nmc_readline (EDITOR_PROMPT_CON_TYPE); type = type_ask = type_ask ? g_strstrip (type_ask) : NULL; - connection_type = check_valid_name_toplevel (type_ask, &err1); + connection_type = check_valid_name_toplevel (type_ask, &slave_type, &err1); g_free (type_ask); } g_free (tmp_str); @@ -7801,7 +7822,7 @@ do_connection_edit (NmCli *nmc, int argc, char **argv) nm_connection_add_setting (connection, NM_SETTING (s_con)); /* Initialize the new connection so that it is valid from the start */ - editor_init_new_connection (nmc, connection); + editor_init_new_connection (nmc, connection, slave_type); } /* nmcli runs the editor */ |