Commit 5d3736ac authored by Thomas Haller's avatar Thomas Haller

cli: drop nmc_strsplit_set()

In most cases, it copies the entire strv needlessly.
We can do better.

Also, the max_tokens argument is handled wrongly (albeit
not used anywhere anymore).
parent 88699435
...@@ -1555,18 +1555,18 @@ get_invisible_active_connections (NmCli *nmc) ...@@ -1555,18 +1555,18 @@ get_invisible_active_connections (NmCli *nmc)
static GArray * static GArray *
parse_preferred_connection_order (const char *order, GError **error) parse_preferred_connection_order (const char *order, GError **error)
{ {
char **strv, **iter; gs_free const char **strv = NULL;
const char *const*iter;
const char *str; const char *str;
GArray *order_arr; GArray *order_arr;
NmcSortOrder val; NmcSortOrder val;
gboolean inverse, unique; gboolean inverse, unique;
int i; int i;
strv = nmc_strsplit_set (order, ":", -1); strv = nm_utils_strsplit_set (order, ":");
if (!strv || !*strv) { if (!strv) {
g_set_error (error, NMCLI_ERROR, 0, g_set_error (error, NMCLI_ERROR, 0,
_("incorrect string '%s' of '--order' option"), order); _("incorrect string '%s' of '--order' option"), order);
g_strfreev (strv);
return NULL; return NULL;
} }
...@@ -1608,7 +1608,6 @@ parse_preferred_connection_order (const char *order, GError **error) ...@@ -1608,7 +1608,6 @@ parse_preferred_connection_order (const char *order, GError **error)
g_array_append_val (order_arr, val); g_array_append_val (order_arr, val);
} }
g_strfreev (strv);
return order_arr; return order_arr;
} }
...@@ -2261,48 +2260,50 @@ activate_connection_cb (GObject *client, GAsyncResult *result, gpointer user_dat ...@@ -2261,48 +2260,50 @@ activate_connection_cb (GObject *client, GAsyncResult *result, gpointer user_dat
static GHashTable * static GHashTable *
parse_passwords (const char *passwd_file, GError **error) parse_passwords (const char *passwd_file, GError **error)
{ {
GHashTable *pwds_hash; gs_unref_hashtable GHashTable *pwds_hash = NULL;
char *contents = NULL; gs_free char *contents = NULL;
gsize len = 0; gsize len = 0;
GError *local_err = NULL; GError *local_err = NULL;
char **lines, **iter; gs_free const char **strv = NULL;
const char *const*iter;
char *pwd_spec, *pwd, *prop; char *pwd_spec, *pwd, *prop;
const char *setting; const char *setting;
pwds_hash = g_hash_table_new_full (nm_str_hash, g_str_equal, g_free, g_free); pwds_hash = g_hash_table_new_full (nm_str_hash, g_str_equal, g_free, g_free);
if (!passwd_file) if (!passwd_file)
return pwds_hash; return g_steal_pointer (&pwds_hash);
/* Read the passwords file */ /* Read the passwords file */
if (!g_file_get_contents (passwd_file, &contents, &len, &local_err)) { if (!g_file_get_contents (passwd_file, &contents, &len, &local_err)) {
g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT,
_("failed to read passwd-file '%s': %s"), _("failed to read passwd-file '%s': %s"),
passwd_file, local_err->message); passwd_file, local_err->message);
g_error_free (local_err); g_error_free (local_err);
g_hash_table_destroy (pwds_hash);
return NULL; return NULL;
} }
lines = nmc_strsplit_set (contents, "\r\n", -1); strv = nm_utils_strsplit_set (contents, "\r\n");
for (iter = lines; *iter; iter++) { for (iter = strv; *iter; iter++) {
pwd = strchr (*iter, ':'); gs_free char *iter_s = g_strdup (*iter);
pwd = strchr (iter_s, ':');
if (!pwd) { if (!pwd) {
g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT,
_("missing colon in 'password' entry '%s'"), *iter); _("missing colon in 'password' entry '%s'"), *iter);
goto failure; return NULL;
} }
*(pwd++) = '\0'; *(pwd++) = '\0';
prop = strchr (*iter, '.'); prop = strchr (iter_s, '.');
if (!prop) { if (!prop) {
g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT,
_("missing dot in 'password' entry '%s'"), *iter); _("missing dot in 'password' entry '%s'"), *iter);
goto failure; return NULL;
} }
*(prop++) = '\0'; *(prop++) = '\0';
setting = *iter; setting = iter_s;
while (g_ascii_isspace (*setting)) while (g_ascii_isspace (*setting))
setting++; setting++;
/* Accept wifi-sec or wifi instead of cumbersome '802-11-wireless-security' */ /* Accept wifi-sec or wifi instead of cumbersome '802-11-wireless-security' */
...@@ -2311,21 +2312,13 @@ parse_passwords (const char *passwd_file, GError **error) ...@@ -2311,21 +2312,13 @@ parse_passwords (const char *passwd_file, GError **error)
if (nm_setting_lookup_type (setting) == G_TYPE_INVALID) { if (nm_setting_lookup_type (setting) == G_TYPE_INVALID) {
g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT,
_("invalid setting name in 'password' entry '%s'"), setting); _("invalid setting name in 'password' entry '%s'"), setting);
goto failure; return NULL;
} }
pwd_spec = g_strdup_printf ("%s.%s", setting, prop); pwd_spec = g_strdup_printf ("%s.%s", setting, prop);
g_hash_table_insert (pwds_hash, pwd_spec, g_strdup (pwd)); g_hash_table_insert (pwds_hash, pwd_spec, g_strdup (pwd));
} }
g_strfreev (lines); return g_steal_pointer (&pwds_hash);
g_free (contents);
return pwds_hash;
failure:
g_strfreev (lines);
g_free (contents);
g_hash_table_destroy (pwds_hash);
return NULL;
} }
......
...@@ -313,16 +313,18 @@ _set_fcn_precheck_connection_secondaries (const char *value, ...@@ -313,16 +313,18 @@ _set_fcn_precheck_connection_secondaries (const char *value,
{ {
const GPtrArray *connections; const GPtrArray *connections;
NMConnection *con; NMConnection *con;
gs_free const char **strv0 = NULL;
gs_strfreev char **strv = NULL; gs_strfreev char **strv = NULL;
char **iter; char **iter;
gboolean modified; gboolean modified = FALSE;
strv = nmc_strsplit_set (value, " \t,", 0); strv0 = nm_utils_strsplit_set (value, " \t,");
if (!strv) if (!strv0)
return TRUE; return TRUE;
connections = nm_client_get_connections (nm_cli.client); connections = nm_client_get_connections (nm_cli.client);
strv = g_strdupv ((char **) strv0);
for (iter = strv; *iter; iter++) { for (iter = strv; *iter; iter++) {
if (nm_utils_is_uuid (*iter)) { if (nm_utils_is_uuid (*iter)) {
con = nmc_find_connection (connections, "uuid", *iter, NULL, FALSE); con = nmc_find_connection (connections, "uuid", *iter, NULL, FALSE);
......
...@@ -603,9 +603,14 @@ int ...@@ -603,9 +603,14 @@ int
nmc_string_to_arg_array (const char *line, const char *delim, gboolean unquote, nmc_string_to_arg_array (const char *line, const char *delim, gboolean unquote,
char ***argv, int *argc) char ***argv, int *argc)
{ {
gs_free const char **arr0 = NULL;
char **arr; char **arr;
arr = nmc_strsplit_set (line ? line : "", delim ? delim : " \t", 0); arr0 = nm_utils_strsplit_set (line ?: "", delim ?: " \t");
if (!arr0)
arr = g_new0 (char *, 1);
else
arr = g_strdupv ((char **) arr0);
if (unquote) { if (unquote) {
int i = 0; int i = 0;
...@@ -613,7 +618,7 @@ nmc_string_to_arg_array (const char *line, const char *delim, gboolean unquote, ...@@ -613,7 +618,7 @@ nmc_string_to_arg_array (const char *line, const char *delim, gboolean unquote,
size_t l; size_t l;
const char *quotes = "\"'"; const char *quotes = "\"'";
while (arr && arr[i]) { while (arr[i]) {
s = arr[i]; s = arr[i];
l = strlen (s); l = strlen (s);
if (l >= 2) { if (l >= 2) {
...@@ -628,7 +633,6 @@ nmc_string_to_arg_array (const char *line, const char *delim, gboolean unquote, ...@@ -628,7 +633,6 @@ nmc_string_to_arg_array (const char *line, const char *delim, gboolean unquote,
*argv = arr; *argv = arr;
*argc = g_strv_length (arr); *argc = g_strv_length (arr);
return 0; return 0;
} }
......
...@@ -53,7 +53,6 @@ int nmc_string_to_arg_array (const char *line, const char *delim, gboolean unquo ...@@ -53,7 +53,6 @@ int nmc_string_to_arg_array (const char *line, const char *delim, gboolean unquo
char ***argv, int *argc); char ***argv, int *argc);
const char *nmc_string_is_valid (const char *input, const char **allowed, GError **error); const char *nmc_string_is_valid (const char *input, const char **allowed, GError **error);
char * nmc_util_strv_for_display (const char *const*strv, gboolean brackets); char * nmc_util_strv_for_display (const char *const*strv, gboolean brackets);
char **nmc_strsplit_set (const char *str, const char *delimiter, int max_tokens);
int nmc_string_screen_width (const char *start, const char *end); int nmc_string_screen_width (const char *start, const char *end);
void set_val_str (NmcOutputField fields_array[], guint32 index, char *value); void set_val_str (NmcOutputField fields_array[], guint32 index, char *value);
void set_val_strc (NmcOutputField fields_array[], guint32 index, const char *value); void set_val_strc (NmcOutputField fields_array[], guint32 index, const char *value);
......
...@@ -173,18 +173,6 @@ finish: ...@@ -173,18 +173,6 @@ finish:
return ret; return ret;
} }
/*
* Wrapper function for g_strsplit_set() that removes empty strings
* from the vector as they are not useful in most cases.
*/
char **
nmc_strsplit_set (const char *str, const char *delimiter, int max_tokens)
{
/* remove empty strings */
return _nm_utils_strv_cleanup (g_strsplit_set (str, delimiter, max_tokens),
FALSE, TRUE, FALSE);
}
gboolean gboolean
matches (const char *cmd, const char *pattern) matches (const char *cmd, const char *pattern)
{ {
......
...@@ -32,8 +32,6 @@ typedef enum { ...@@ -32,8 +32,6 @@ typedef enum {
const char *nmc_string_is_valid (const char *input, const char **allowed, GError **error); const char *nmc_string_is_valid (const char *input, const char **allowed, GError **error);
char **nmc_strsplit_set (const char *str, const char *delimiter, int max_tokens);
gboolean nmc_string_to_uint (const char *str, gboolean nmc_string_to_uint (const char *str,
gboolean range_check, gboolean range_check,
unsigned long int min, unsigned long int min,
......
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