Factor out more common code in opts parsing.

This commit is contained in:
Ian Gulliver
2016-02-20 23:31:57 -08:00
parent 9df778a88a
commit 0e8dd4007c

View File

@@ -19,6 +19,31 @@ static char *opts_split(char **arg, char delim) {
return ret;
}
static void opts_add_listen(char *host_port, incoming_connection_handler handler, void *passthrough) {
char *host = opts_split(&host_port, '/');
if (host) {
incoming_new(host, host_port, handler, passthrough);
free(host);
} else {
incoming_new(NULL, host_port, handler, passthrough);
}
}
static struct serializer *opts_get_serializer(char **arg) {
char *format = opts_split(arg, '=');
if (!format) {
return NULL;
}
struct serializer *serializer = send_get_serializer(format);
free(format);
if (!serializer) {
return NULL;
}
return serializer;
}
bool opts_add_dump(char *arg) {
struct serializer *serializer = send_get_serializer(arg);
if (!serializer) {
@@ -40,58 +65,32 @@ bool opts_add_connect_receive(char *arg) {
}
bool opts_add_connect_send(char *arg) {
char *format = opts_split(&arg, '=');
if (!format) {
return false;
}
struct serializer *serializer = send_get_serializer(format);
struct serializer *serializer = opts_get_serializer(&arg);
if (!serializer) {
free(format);
return false;
}
char *host = opts_split(&arg, '/');
if (!host) {
free(format);
return false;
}
incoming_new(host, arg, send_add_wrapper, serializer);
free(format);
free(host);
return true;
}
bool opts_add_listen_receive(char *arg) {
char *host = opts_split(&arg, '/');
if (host) {
incoming_new(host, arg, receive_new, NULL);
free(host);
} else {
incoming_new(NULL, arg, receive_new, NULL);
}
opts_add_listen(arg, receive_new, NULL);
return true;
}
bool opts_add_listen_send(char *arg) {
char *format = opts_split(&arg, '=');
if (!format) {
return false;
}
struct serializer *serializer = send_get_serializer(format);
struct serializer *serializer = opts_get_serializer(&arg);
if (!serializer) {
free(format);
return false;
}
char *host = opts_split(&arg, '/');
if (host) {
incoming_new(host, arg, send_add_wrapper, serializer);
free(host);
} else {
incoming_new(NULL, arg, send_add_wrapper, serializer);
}
opts_add_listen(arg, send_add_wrapper, serializer);
return true;
}