Commit e63f835d authored by Thomas Haller's avatar Thomas Haller

cli,all: merge branch 'th/strsplit-pt4'

!117

(cherry picked from commit df3c7c3f)
parents 30bb93cd 5b2d0f0f
Pipeline #31699 failed with stage
in 46 minutes and 3 seconds
This diff is collapsed.
......@@ -280,8 +280,8 @@ struct _NMMetaPropertyTypData {
void (*remove_by_idx_fcn_u) (NMSetting *setting, guint idx);
void (*remove_by_idx_fcn_s) (NMSetting *setting, int idx);
gboolean (*remove_by_value_fcn) (NMSetting *setting, const char *item);
bool strsplit_with_escape:1;
bool strsplit_escaped_tokens:1;
bool strsplit_plain:1;
bool strsplit_with_spaces:1;
} multilist;
struct {
guint (*get_num_fcn) (NMSetting *setting);
......@@ -297,8 +297,7 @@ struct _NMMetaPropertyTypData {
void (*remove_by_idx_fcn_u) (NMSetting *setting, guint idx);
void (*remove_by_idx_fcn_s) (NMSetting *setting, int idx);
bool delimit_pretty_with_semicolon:1;
bool strsplit_with_escape:1;
bool strsplit_escaped_tokens:1;
bool strsplit_plain:1;
} objlist;
struct {
gboolean (*set_fcn) (NMSetting *setting,
......@@ -438,7 +437,7 @@ struct _NMMetaType {
NMMetaAccessorGetType get_type,
NMMetaAccessorGetFlags get_flags,
NMMetaAccessorGetOutFlags *out_flags,
gboolean *out_is_defalt,
gboolean *out_is_default,
gpointer *out_to_free);
const char *const*(*complete_fcn) (const NMMetaAbstractInfo *info,
const NMMetaEnvironment *environment,
......
......@@ -8074,102 +8074,6 @@ test_ethtool_offload (void)
g_assert_cmpstr (d->optname, ==, NM_ETHTOOL_OPTNAME_FEATURE_RXHASH);
}
static void
test_nm_utils_escape_spaces (void)
{
char *to_free;
g_assert_cmpstr (_nm_utils_escape_spaces (NULL, &to_free), ==, NULL);
g_free (to_free);
g_assert_cmpstr (_nm_utils_escape_spaces ("", &to_free), ==, "");
g_free (to_free);
g_assert_cmpstr (_nm_utils_escape_spaces (" ", &to_free), ==, "\\ ");
g_free (to_free);
g_assert_cmpstr (_nm_utils_escape_spaces ("\t ", &to_free), ==, "\\\t\\ ");
g_free (to_free);
g_assert_cmpstr (_nm_utils_escape_spaces ("abc", &to_free), ==, "abc");
g_free (to_free);
g_assert_cmpstr (_nm_utils_escape_spaces ("abc def", &to_free), ==, "abc\\ def");
g_free (to_free);
g_assert_cmpstr (_nm_utils_escape_spaces ("abc\tdef", &to_free), ==, "abc\\\tdef");
g_free (to_free);
}
static void
_do_test_unescape_spaces (const char *in, const char *out)
{
nm_auto_free_gstring GString *str_out = g_string_new (NULL);
nm_auto_free_gstring GString *str_in = g_string_new (NULL);
guint i;
for (i = 0; i < 10; i++) {
g_string_set_size (str_in, 0);
g_string_append (str_in, in);
if (i == 0)
g_assert_cmpstr (_nm_utils_unescape_spaces (str_in->str, FALSE), ==, out);
else if (i == 1)
g_assert_cmpstr (_nm_utils_unescape_spaces (str_in->str, TRUE), ==, out);
else {
bool do_strip = nmtst_get_rand_bool ();
guint n = nmtst_get_rand_int () % 20;
guint j;
g_string_set_size (str_out, 0);
if (!do_strip)
g_string_append (str_out, out);
for (j = 0; j < n; j++) {
gboolean append = nmtst_get_rand_bool ();
char ch = nmtst_rand_select (' ', '\t');
if (append && out[0] && out[strlen (out) - 1] == '\\')
append = FALSE;
g_string_insert_c (str_in, append ? -1 : 0, ch);
if (!do_strip)
g_string_insert_c (str_out, append ? -1 : 0, ch);
}
if (do_strip)
g_assert_cmpstr (_nm_utils_unescape_spaces (str_in->str, TRUE), ==, out);
else
g_assert_cmpstr (_nm_utils_unescape_spaces (str_in->str, FALSE), ==, str_out->str);
}
}
}
static void
test_nm_utils_unescape_spaces (void)
{
_do_test_unescape_spaces ("", "");
_do_test_unescape_spaces ("\\", "\\");
_do_test_unescape_spaces ("\\ ", " ");
_do_test_unescape_spaces ("\\\t", "\t");
_do_test_unescape_spaces ("a", "a");
_do_test_unescape_spaces ("\\a", "\\a");
_do_test_unescape_spaces ("foobar", "foobar");
_do_test_unescape_spaces ("foo bar", "foo bar");
_do_test_unescape_spaces ("foo\\ bar", "foo bar");
_do_test_unescape_spaces ("foo\\", "foo\\");
_do_test_unescape_spaces ("\\\\", "\\\\");
_do_test_unescape_spaces ("foo bar", "foo bar");
_do_test_unescape_spaces ("\\ foo bar", " foo bar");
_do_test_unescape_spaces ("\\ foo bar\\ ", " foo bar ");
_do_test_unescape_spaces ("\\\tfoo bar\\\t", "\tfoo bar\t");
_do_test_unescape_spaces ("\\\tfoo bar \\\t", "\tfoo bar \t");
_do_test_unescape_spaces ("\\\t", "\t");
_do_test_unescape_spaces ("\\\t \\ ", "\t ");
}
/*****************************************************************************/
NMTST_DEFINE ();
......@@ -8323,8 +8227,6 @@ int main (int argc, char **argv)
g_test_add_func ("/core/general/_nm_utils_dns_option_find_idx", test_nm_utils_dns_option_find_idx);
g_test_add_func ("/core/general/_nm_utils_validate_json", test_nm_utils_check_valid_json);
g_test_add_func ("/core/general/_nm_utils_team_config_equal", test_nm_utils_team_config_equal);
g_test_add_func ("/core/general/_nm_utils_escape_spaces", test_nm_utils_escape_spaces);
g_test_add_func ("/core/general/_nm_utils_unescape_spaces", test_nm_utils_unescape_spaces);
g_test_add_func ("/core/general/test_nm_utils_enum", test_nm_utils_enum);
g_test_add_func ("/core/general/nm-set-out", test_nm_set_out);
g_test_add_func ("/core/general/route_attributes/parse", test_route_attributes_parse);
......
......@@ -2618,81 +2618,6 @@ _nm_utils_user_data_unpack (gpointer user_data, int nargs, ...)
/*****************************************************************************/
const char *
_nm_utils_escape_plain (const char *str, const char *candidates, char **to_free)
{
const char *ptr = str;
char *ret, *r;
guint8 ch_lookup[256];
*to_free = NULL;
if (!str)
return NULL;
_char_lookup_table_init (ch_lookup, candidates ?: NM_ASCII_SPACES);
while (TRUE) {
if (!*ptr)
return str;
if (_char_lookup_has (ch_lookup, *ptr))
break;
ptr++;
}
ptr = str;
ret = g_new (char, strlen (str) * 2 + 1);
r = ret;
*to_free = ret;
while (*ptr) {
if (_char_lookup_has (ch_lookup, *ptr))
*r++ = '\\';
*r++ = *ptr++;
}
*r = '\0';
return ret;
}
char *
_nm_utils_unescape_plain (char *str, const char *candidates, gboolean do_strip)
{
gsize i = 0;
gsize j = 0;
gsize preserve_space_at = 0;
guint8 ch_lookup[256];
if (!str)
return NULL;
_char_lookup_table_init (ch_lookup, candidates ?: NM_ASCII_SPACES);
if (do_strip) {
while (str[i] && _char_lookup_has (ch_lookup, str[i]))
i++;
}
for (; str[i]; i++) {
if ( str[i] == '\\'
&& _char_lookup_has (ch_lookup, str[i+1])) {
preserve_space_at = j;
i++;
}
str[j++] = str[i];
}
str[j] = '\0';
if (do_strip && j > 0) {
while ( --j > preserve_space_at
&& _char_lookup_has (ch_lookup, str[j]))
str[j] = '\0';
}
return str;
}
/*****************************************************************************/
typedef struct {
gpointer callback_user_data;
GCancellable *cancellable;
......
......@@ -1110,23 +1110,6 @@ void _nm_utils_user_data_unpack (gpointer user_data, int nargs, ...);
/*****************************************************************************/
const char *_nm_utils_escape_plain (const char *str, const char *candidates, char **to_free);
char *_nm_utils_unescape_plain (char *str, const char *candidates, gboolean do_strip);
static inline const char *
_nm_utils_escape_spaces (const char *str, char **to_free)
{
return _nm_utils_escape_plain (str, NM_ASCII_SPACES, to_free);
}
static inline char *
_nm_utils_unescape_spaces (char *str, gboolean do_strip)
{
return _nm_utils_unescape_plain (str, NM_ASCII_SPACES, do_strip);
}
/*****************************************************************************/
typedef void (*NMUtilsInvokeOnIdleCallback) (gpointer callback_user_data,
GCancellable *cancellable);
......
......@@ -1427,13 +1427,12 @@ make_match_setting (shvarFile *ifcfg)
if (!v)
return NULL;
strv = nm_utils_strsplit_set_full (v, " \t", NM_UTILS_STRSPLIT_SET_FLAGS_ALLOW_ESCAPING);
strv = nm_utils_escaped_tokens_split (v, NM_ASCII_SPACES);
if (strv) {
for (i = 0; strv[i]; i++) {
if (!s_match)
s_match = (NMSettingMatch *) nm_setting_match_new ();
nm_setting_match_add_interface_name (s_match,
_nm_utils_unescape_spaces ((char *) strv[i], TRUE));
nm_setting_match_add_interface_name (s_match, strv[i]);
}
}
......
......@@ -2375,15 +2375,17 @@ write_match_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
num = nm_setting_match_get_num_interface_names (s_match);
for (i = 0; i < num; i++) {
gs_free char *to_free = NULL;
const char *name;
if (i == 0)
name = nm_setting_match_get_interface_name (s_match, i);
if (!name || !name[0])
continue;
if (!str)
str = g_string_new ("");
else
g_string_append_c (str, ' ');
name = nm_setting_match_get_interface_name (s_match, i);
g_string_append (str, _nm_utils_escape_spaces (name, &to_free));
nm_utils_escaped_tokens_escape_gstr (name, NM_ASCII_SPACES, str);
}
if (str)
......
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