Skip to content

Commit

Permalink
Simplify target string parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
donald committed Jun 9, 2022
1 parent c4141ba commit c03dc19
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions cmirror.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c03dc19

Please sign in to comment.