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)
static GArray *
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;
GArray *order_arr;
NmcSortOrder val;
gboolean inverse, unique;
int i;
strv = nmc_strsplit_set (order, ":", -1);
if (!strv || !*strv) {
strv = nm_utils_strsplit_set (order, ":");
if (!strv) {
g_set_error (error, NMCLI_ERROR, 0,
_("incorrect string '%s' of '--order' option"), order);
g_strfreev (strv);
return NULL;
}
......@@ -1608,7 +1608,6 @@ parse_preferred_connection_order (const char *order, GError **error)
g_array_append_val (order_arr, val);
}
g_strfreev (strv);
return order_arr;
}
......@@ -2261,18 +2260,19 @@ activate_connection_cb (GObject *client, GAsyncResult *result, gpointer user_dat
static GHashTable *
parse_passwords (const char *passwd_file, GError **error)
{
GHashTable *pwds_hash;
char *contents = NULL;
gs_unref_hashtable GHashTable *pwds_hash = NULL;
gs_free char *contents = NULL;
gsize len = 0;
GError *local_err = NULL;
char **lines, **iter;
gs_free const char **strv = NULL;
const char *const*iter;
char *pwd_spec, *pwd, *prop;
const char *setting;
pwds_hash = g_hash_table_new_full (nm_str_hash, g_str_equal, g_free, g_free);
if (!passwd_file)
return pwds_hash;
return g_steal_pointer (&pwds_hash);
/* Read the passwords file */
if (!g_file_get_contents (passwd_file, &contents, &len, &local_err)) {
......@@ -2280,29 +2280,30 @@ parse_passwords (const char *passwd_file, GError **error)
_("failed to read passwd-file '%s': %s"),
passwd_file, local_err->message);
g_error_free (local_err);
g_hash_table_destroy (pwds_hash);
return NULL;
}
lines = nmc_strsplit_set (contents, "\r\n", -1);
for (iter = lines; *iter; iter++) {
pwd = strchr (*iter, ':');
strv = nm_utils_strsplit_set (contents, "\r\n");
for (iter = strv; *iter; iter++) {
gs_free char *iter_s = g_strdup (*iter);
pwd = strchr (iter_s, ':');
if (!pwd) {
g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT,
_("missing colon in 'password' entry '%s'"), *iter);
goto failure;
return NULL;
}
*(pwd++) = '\0';
prop = strchr (*iter, '.');
prop = strchr (iter_s, '.');
if (!prop) {
g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT,
_("missing dot in 'password' entry '%s'"), *iter);
goto failure;
return NULL;
}
*(prop++) = '\0';
setting = *iter;
setting = iter_s;
while (g_ascii_isspace (*setting))
setting++;
/* Accept wifi-sec or wifi instead of cumbersome '802-11-wireless-security' */
......@@ -2311,21 +2312,13 @@ parse_passwords (const char *passwd_file, GError **error)
if (nm_setting_lookup_type (setting) == G_TYPE_INVALID) {
g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT,
_("invalid setting name in 'password' entry '%s'"), setting);
goto failure;
return NULL;
}
pwd_spec = g_strdup_printf ("%s.%s", setting, prop);
g_hash_table_insert (pwds_hash, pwd_spec, g_strdup (pwd));
}
g_strfreev (lines);
g_free (contents);
return pwds_hash;
failure:
g_strfreev (lines);
g_free (contents);
g_hash_table_destroy (pwds_hash);
return NULL;
return g_steal_pointer (&pwds_hash);
}
......
......@@ -313,16 +313,18 @@ _set_fcn_precheck_connection_secondaries (const char *value,
{
const GPtrArray *connections;
NMConnection *con;
gs_free const char **strv0 = NULL;
gs_strfreev char **strv = NULL;
char **iter;
gboolean modified;
gboolean modified = FALSE;
strv = nmc_strsplit_set (value, " \t,", 0);
if (!strv)
strv0 = nm_utils_strsplit_set (value, " \t,");
if (!strv0)
return TRUE;
connections = nm_client_get_connections (nm_cli.client);
strv = g_strdupv ((char **) strv0);
for (iter = strv; *iter; iter++) {
if (nm_utils_is_uuid (*iter)) {
con = nmc_find_connection (connections, "uuid", *iter, NULL, FALSE);
......
......@@ -603,9 +603,14 @@ int
nmc_string_to_arg_array (const char *line, const char *delim, gboolean unquote,
char ***argv, int *argc)
{
gs_free const char **arr0 = NULL;
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) {
int i = 0;
......@@ -613,7 +618,7 @@ nmc_string_to_arg_array (const char *line, const char *delim, gboolean unquote,
size_t l;
const char *quotes = "\"'";
while (arr && arr[i]) {
while (arr[i]) {
s = arr[i];
l = strlen (s);
if (l >= 2) {
......@@ -628,7 +633,6 @@ nmc_string_to_arg_array (const char *line, const char *delim, gboolean unquote,
*argv = arr;
*argc = g_strv_length (arr);
return 0;
}
......
......@@ -53,7 +53,6 @@ int nmc_string_to_arg_array (const char *line, const char *delim, gboolean unquo
char ***argv, int *argc);
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_strsplit_set (const char *str, const char *delimiter, int max_tokens);
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_strc (NmcOutputField fields_array[], guint32 index, const char *value);
......
......@@ -173,18 +173,6 @@ finish:
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
matches (const char *cmd, const char *pattern)
{
......
......@@ -32,8 +32,6 @@ typedef enum {
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 range_check,
unsigned long int min,
......
......@@ -117,7 +117,6 @@ _parse_ip_route (int family,
GError **error)
{
const int MAX_PREFIX = (family == AF_INET) ? 32 : 128;
char *plen = NULL;
const char *next_hop = NULL;
const char *canon_dest;
int prefix;
......@@ -125,9 +124,11 @@ _parse_ip_route (int family,
GError *local = NULL;
gint64 metric = -1;
guint i;
gs_strfreev char **routev = NULL;
gs_free const char **routev = NULL;
gs_free char *str_clean = NULL;
char *dest;
gs_free char *dest_clone = NULL;
const char *dest;
const char *plen;
gs_unref_hashtable GHashTable *attrs = NULL;
GHashTable *tmp_attrs;
#define ROUTE_SYNTAX _("The valid syntax is: 'ip[/prefix] [next-hop] [metric] [attribute=val]... [,ip[/prefix] ...]'")
......@@ -137,8 +138,8 @@ _parse_ip_route (int family,
nm_assert (!error || !*error);
str_clean = g_strstrip (g_strdup (str));
routev = nmc_strsplit_set (str_clean, " \t", 0);
if (!routev || !routev[0]) {
routev = nm_utils_strsplit_set (str_clean, " \t");
if (!routev) {
g_set_error (error, 1, 0,
"'%s' is not valid. %s",
str, ROUTE_SYNTAX);
......@@ -147,8 +148,13 @@ _parse_ip_route (int family,
dest = routev[0];
plen = strchr (dest, '/'); /* prefix delimiter */
if (plen)
*plen++ = '\0';
if (plen) {
dest_clone = g_strdup (dest);
plen = &dest_clone[plen - dest];
dest = dest_clone;
*((char *) plen) = '\0';
plen++;
}
prefix = MAX_PREFIX;
if (plen) {
if ((prefix = _nm_utils_ascii_str_to_int64 (plen, 10, 1, MAX_PREFIX, -1)) == -1) {
......@@ -295,7 +301,7 @@ static NMTeamLinkWatcher *
_parse_team_link_watcher (const char *str,
GError **error)
{
gs_strfreev char **watcherv = NULL;
gs_free const char **watcherv = NULL;
gs_free char *str_clean = NULL;
guint i;
gs_free const char *name = NULL;
......@@ -308,17 +314,17 @@ _parse_team_link_watcher (const char *str,
nm_assert (!error || !*error);
str_clean = g_strstrip (g_strdup (str));
watcherv = nmc_strsplit_set (str_clean, " \t", 0);
if (!watcherv || !watcherv[0]) {
watcherv = nm_utils_strsplit_set (str_clean, " \t");
if (!watcherv) {
g_set_error (error, 1, 0, "'%s' is not valid", str);
return NULL;
}
for (i = 0; watcherv[i]; i++) {
gs_strfreev char **pair = NULL;
gs_free const char **pair = NULL;
pair = nmc_strsplit_set (watcherv[i], "=", 0);
if (!pair[0]) {
pair = nm_utils_strsplit_set (watcherv[i], "=");
if (!pair) {
g_set_error (error, 1, 0, "'%s' is not valid: %s", watcherv[i],
"properties should be specified as 'key=value'");
return NULL;
......@@ -1170,22 +1176,21 @@ _set_fcn_gobject_mac (ARGS_SET_FCN)
static gboolean
_set_fcn_gobject_secret_flags (ARGS_SET_FCN)
{
char **strv = NULL, **iter;
gs_free const char **strv = NULL;
const char **iter;
unsigned long flags = 0, val_int;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
nm_assert (!error || !*error);
strv = nmc_strsplit_set (value, " \t,", 0);
strv = nm_utils_strsplit_set (value, " \t,");
for (iter = strv; iter && *iter; iter++) {
if (!nmc_string_to_uint (*iter, TRUE, 0, ALL_SECRET_FLAGS, &val_int)) {
g_set_error (error, 1, 0, _("'%s' is not a valid flag number; use <0-%d>"),
*iter, ALL_SECRET_FLAGS);
g_strfreev (strv);
return FALSE;
}
flags += val_int;
}
g_strfreev (strv);
/* Validate the flags number */
if (flags > ALL_SECRET_FLAGS) {
......@@ -1572,18 +1577,19 @@ vpn_data_item (const char *key, const char *value, gpointer user_data)
const char **valid_strv, \
GError **error) \
{ \
char **strv = NULL, **iter; \
gs_free const char **strv = NULL; \
gsize i; \
const char *item; \
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); \
strv = nmc_strsplit_set (value, " \t,", 0); \
for (iter = strv; iter && *iter; iter++) { \
if (!(item = nmc_string_is_valid (g_strstrip (*iter), valid_strv, error))) { \
g_strfreev (strv); \
nm_assert (!error || !*error); \
strv = nm_utils_strsplit_set (value, " \t,"); \
if (strv) { \
for (i = 0; strv[i]; i++) { \
if (!(item = nmc_string_is_valid (strv[i], valid_strv, error))) { \
return FALSE; \
} \
set_func (s_macro (setting), item); \
} \
g_strfreev (strv); \
} \
return TRUE; \
}
......@@ -1591,43 +1597,42 @@ vpn_data_item (const char *key, const char *value, gpointer user_data)
static gboolean \
def_func (ARGS_SET_FCN) \
{ \
char **strv = NULL, **iter; \
gs_free const char **strv = NULL; \
const char **iter; \
const char **(*valid_func1_p) (s_type *) = valid_func1; \
const char * (*valid_func2_p) (const char *, const char *, GError **) = valid_func2; \
const char *opt_name, *opt_val; \
\
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); \
nm_assert (!error || !*error); \
\
strv = nmc_strsplit_set (value, ",", 0); \
strv = nm_utils_strsplit_set (value, ","); \
for (iter = strv; iter && *iter; iter++) { \
char *left = g_strstrip (*iter); \
gs_free char *left_clone = g_strstrip (g_strdup (*iter)); \
char *left = left_clone; \
char *right = strchr (left, '='); \
if (!right) { \
g_set_error (error, 1, 0, _("'%s' is not valid; use <option>=<value>"), *iter); \
g_strfreev (strv); \
return FALSE; \
} \
*right++ = '\0'; \
g_strchomp (left); \
\
if (valid_func1_p) { \
const char **valid_options = valid_func1_p (s_macro (setting)); \
if (!(opt_name = nmc_string_is_valid (g_strstrip (left), valid_options, error))) { \
g_strfreev (strv); \
if (!(opt_name = nmc_string_is_valid (left, valid_options, error))) { \
return FALSE; \
} \
} else \
opt_name = g_strstrip (left);\
opt_name = left;\
\
opt_val = g_strstrip (right); \
opt_val = g_strchug (right); \
if (valid_func2_p) { \
if (!(opt_val = valid_func2_p ((const char *) left, (const char *) opt_val, error))) { \
g_strfreev (strv); \
return FALSE; \
}\
}\
add_func (s_macro (setting), opt_name, opt_val); \
} \
g_strfreev (strv); \
return TRUE; \
}
......@@ -1682,40 +1687,38 @@ vpn_data_item (const char *key, const char *value, gpointer user_data)
def_func (ARGS_SET_FCN) \
{ \
guint8 buf[32]; \
char **list = NULL, **iter; \
GSList *macaddr_blacklist = NULL; \
gs_free const char **strv = NULL; \
const char *const*iter; \
\
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); \
nm_assert (!error || !*error); \
\
list = nmc_strsplit_set (value, " \t,", 0); \
for (iter = list; iter && *iter; iter++) { \
strv = nm_utils_strsplit_set (value, " \t,"); \
for (iter = strv; strv && *iter; iter++) { \
if (!nm_utils_hwaddr_aton (*iter, buf, ETH_ALEN)) { \
g_set_error (error, 1, 0, _("'%s' is not a valid MAC"), *iter); \
g_strfreev (list); \
g_slist_free (macaddr_blacklist); \
return FALSE; \
} \
} \
\
for (iter = list; iter && *iter; iter++) \
for (iter = strv; strv && *iter; iter++) \
add_func (s_macro (setting), *iter); \
\
g_strfreev (list); \
return TRUE; \
}
static gboolean
verify_string_list (char **strv,
verify_string_list (const char *const*strv,
const char *prop,
gboolean (*validate_func) (const char *),
GError **error)
{
char **iter;
const char *const*iter;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
nm_assert (!error || !*error);
for (iter = strv; iter && *iter; iter++) {
if (strv) {
for (iter = strv; *iter; iter++) {
if (**iter == '\0')
continue;
if (validate_func) {
......@@ -1726,6 +1729,7 @@ verify_string_list (char **strv,
}
}
}
}
return TRUE;
}
......@@ -1778,7 +1782,7 @@ check_and_set_string (NMSetting *setting,
{
const char *checked_val;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
nm_assert (!error || !*error);
checked_val = nmc_string_is_valid (val, valid_strv, error);
if (!checked_val)
......@@ -1793,7 +1797,7 @@ _set_fcn_gobject_flags (ARGS_SET_FCN)
{
unsigned long val_int;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
nm_assert (!error || !*error);
if (!nmc_string_to_uint (value, TRUE, 0, G_MAXUINT, &val_int)) {
g_set_error (error, 1, 0, _("'%s' is not a valid number (or out of range)"), value);
......@@ -1813,7 +1817,7 @@ _set_fcn_gobject_ssid (ARGS_SET_FCN)
{
GBytes *ssid;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
nm_assert (!error || !*error);
if (strlen (value) > 32) {
g_set_error (error, 1, 0, _("'%s' is not valid"), value);
......@@ -1829,7 +1833,7 @@ _set_fcn_gobject_ssid (ARGS_SET_FCN)
static gboolean
_set_fcn_gobject_ifname (ARGS_SET_FCN)
{
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
nm_assert (!error || !*error);
if (!nm_utils_is_valid_iface_name (value, error))
return FALSE;
......@@ -1891,14 +1895,13 @@ static gboolean
nmc_property_set_bytes (NMSetting *setting, const char *prop, const char *value, GError **error)
{
gs_free char *val_strip = NULL;
gs_strfreev char **strv = NULL;
const char *delimiters = " \t,";
char **iter;
gs_free const char **strv = NULL;
const char **iter;
GBytes *bytes;
GByteArray *array = NULL;
gboolean success = TRUE;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
nm_assert (!error || !*error);
val_strip = g_strstrip (g_strdup (value));
......@@ -1908,8 +1911,8 @@ nmc_property_set_bytes (NMSetting *setting, const char *prop, const char *value,
goto done;
/* Otherwise, consider the following format: AA b 0xCc D */
strv = nmc_strsplit_set (val_strip, delimiters, 0);
array = g_byte_array_sized_new (g_strv_length (strv));
strv = nm_utils_strsplit_set (val_strip, " \t");
array = g_byte_array_sized_new (NM_PTRARRAY_LEN (strv));
for (iter = strv; iter && *iter; iter++) {
int v;
guint8 v8;
......@@ -2113,15 +2116,16 @@ _get_fcn_802_1x_phase2_private_key (ARGS_GET_FCN)
static gboolean \
def_func (ARGS_SET_FCN) \
{ \
char **strv = NULL; \
guint i = 0; \
const char **strv = NULL; \
gsize i; \
\
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); \
nm_assert (error == NULL || *error == NULL); \
\
strv = nmc_strsplit_set (value, " \t,", 0); \
while (strv && strv[i]) \
strv = nm_utils_strsplit_set (value, " \t,"); \
if (strv) { \
for (i = 0; strv[i]; i++) \
set_func (NM_SETTING_802_1X (setting), strv[i++]); \
g_strfreev (strv); \
} \
return TRUE; \
}
......@@ -2500,25 +2504,20 @@ permissions_valid (const char *perm)
static gboolean
_set_fcn_connection_permissions (ARGS_SET_FCN)
{
char **strv = NULL;
guint i = 0;
gs_free const char **strv = NULL;
gsize i;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
nm_assert (!error || !*error);
strv = nmc_strsplit_set (value, " \t,", 0);
if (!verify_string_list (strv, property_info->property_name, permissions_valid, error)) {
g_strfreev (strv);
strv = nm_utils_strsplit_set (value, " \t,");
if (!verify_string_list (strv, property_info->property_name, permissions_valid, error))
return FALSE;
}
for (i = 0; strv && strv[i]; i++) {
const char *user;
if (strncmp (strv[i], PERM_USER_PREFIX, strlen (PERM_USER_PREFIX)) == 0)
user = strv[i]+strlen (PERM_USER_PREFIX);
else
user = strv[i];
const char *user = strv[i];
if (strncmp (user, PERM_USER_PREFIX, NM_STRLEN (PERM_USER_PREFIX)) == 0)
user += NM_STRLEN (PERM_USER_PREFIX);
nm_setting_connection_add_permission (NM_SETTING_CONNECTION (setting), "user", user, NULL);
}
......@@ -2546,7 +2545,7 @@ DEFINE_REMOVER_INDEX_OR_VALUE (_remove_fcn_connection_permissions,
static gboolean
_set_fcn_connection_master (ARGS_SET_FCN)
{
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
nm_assert (!error || !*error);
if (!value)
;
......@@ -2632,16 +2631,14 @@ _complete_fcn_connection_master (ARGS_COMPLETE_FCN)
static gboolean
_set_fcn_connection_secondaries (ARGS_SET_FCN)
{
gs_strfreev char **strv = NULL;
char **iter;
gs_free const char **strv = NULL;
const char *const*iter;
strv = nmc_strsplit_set (value, " \t,", 0);
strv = nm_utils_strsplit_set (value, " \t,");
if (strv) {
for (iter = strv; *iter; iter++) {
if (**iter)
for (iter = strv; *iter; iter++)
nm_setting_connection_add_secondary (NM_SETTING_CONNECTION (setting), *iter);
}
}
return TRUE;
}
......@@ -2827,19 +2824,21 @@ DEFINE_DCB_UINT_GETTER (_get_fcn_dcb_priority_traffic_class, nm_setting_dcb_get_
static gboolean
_set_fcn_dcb_flags (ARGS_SET_FCN)
{
char **strv = NULL, **iter;
NMSettingDcbFlags flags = NM_SETTING_DCB_FLAG_NONE;
long int t;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
nm_assert (!error || !*error);
/* Check for overall hex numeric value */
t = _nm_utils_ascii_str_to_int64 (value, 0, 0, DCB_ALL_FLAGS, -1);
if (t != -1)
flags = (guint) t;
else {
gs_free const char **strv = NULL;
const char *const*iter;
/* Check for individual flag numbers */
strv = nmc_strsplit_set (value, " \t,", 0);
strv = nm_utils_strsplit_set (value, " \t,");
for (iter = strv; iter && *iter; iter++) {
t = _nm_utils_ascii_str_to_int64 (*iter, 0, 0, DCB_ALL_FLAGS, -1);
......@@ -2862,7 +2861,6 @@ _set_fcn_dcb_flags (ARGS_SET_FCN)
return FALSE;
}
}
g_strfreev (strv);