From c03dc19cd5d66a1d09ff95f0890fa3d504e92637 Mon Sep 17 00:00:00 2001 From: Donald Buczek Date: Thu, 9 Jun 2022 10:50:34 +0200 Subject: [PATCH] Simplify target string parsing --- cmirror.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/cmirror.c b/cmirror.c index acec82b..d3acfcf 100644 --- a/cmirror.c +++ b/cmirror.c @@ -1395,6 +1395,8 @@ static void master(char *master_path, char *slave_user, char *slave, char *slave } } +#define STEAL_POINTER(SRC) ({ void *tmp=*(SRC); *(SRC)=NULL; tmp; }) + int main(int argc_cmdline, char **argv_cmdline) { GOptionContext *context = g_option_context_new ("blabla bla"); @@ -1458,32 +1460,22 @@ int main(int argc_cmdline, char **argv_cmdline) { LOCAL_DEV = g_hash_table_new_full(g_int64_hash, g_int64_equal, g_free, NULL); { - GRegex *regex = g_regex_new ("^([^:]+):(.+)$", 0, 0, NULL); // system:/path - GMatchInfo *match_info; - g_regex_match (regex, target, 0, &match_info); - if (g_match_info_matches (match_info)) { - slave = g_match_info_fetch(match_info, 1); - slave_path = g_match_info_fetch(match_info, 2); - GRegex *regex2 = g_regex_new ("^([^@]+)@(.+)$", 0, 0, NULL); // user@system - GMatchInfo *match_info2; - g_regex_match (regex2, slave, 0, &match_info2); - if (g_match_info_matches (match_info2)) { - slave_user = g_match_info_fetch(match_info2, 1); - char *new_slave = g_match_info_fetch(match_info2, 2); - free(slave); - slave = new_slave; + g_auto(GStrv) match1 = MATCH_RE("^([^:]+):(.+)$", target); // system:/path + if (match1) { + slave_path = STEAL_POINTER(&(match1[2])); + g_auto(GStrv) match2 = MATCH_RE("^([^@]+)@(.+)$", match1[1]); // user@system + if (match2) { + slave_user = STEAL_POINTER(&match2[1]); + slave = STEAL_POINTER(&match2[2]); } else { slave_user = g_strdup("root"); + slave = STEAL_POINTER(&match1[1]); } - g_match_info_free(match_info2); - g_regex_unref(regex2); } else { slave = g_strdup(""); slave_user = g_strdup("root"); slave_path = g_strdup(target); } - g_match_info_free(match_info); - g_regex_unref(regex); } if (unix_socket) {