Move FORMAT parsing into send code.
This commit is contained in:
@@ -177,11 +177,11 @@ static bool exec_receive(const char *arg) {
|
||||
}
|
||||
|
||||
static bool exec_send(const char *arg) {
|
||||
return opts_add_send(exec_add, send_flow, arg);
|
||||
return send_add(exec_add, send_flow, arg);
|
||||
}
|
||||
|
||||
static bool exec_send_receive(const char *arg) {
|
||||
return opts_add_send(exec_add, send_receive_flow, arg);
|
||||
return send_add(exec_add, send_receive_flow, arg);
|
||||
}
|
||||
|
||||
void exec_opts_add() {
|
||||
|
||||
@@ -145,19 +145,19 @@ static bool file_read(const char *arg) {
|
||||
}
|
||||
|
||||
static bool file_write(const char *arg) {
|
||||
return opts_add_send(file_write_add, send_flow, arg);
|
||||
return send_add(file_write_add, send_flow, arg);
|
||||
}
|
||||
|
||||
static bool file_write_read(const char *arg) {
|
||||
return opts_add_send(file_write_add, send_receive_flow, arg);
|
||||
return send_add(file_write_add, send_receive_flow, arg);
|
||||
}
|
||||
|
||||
static bool file_append(const char *arg) {
|
||||
return opts_add_send(file_append_add, send_flow, arg);
|
||||
return send_add(file_append_add, send_flow, arg);
|
||||
}
|
||||
|
||||
static bool file_append_read(const char *arg) {
|
||||
return opts_add_send(file_append_add, send_receive_flow, arg);
|
||||
return send_add(file_append_add, send_receive_flow, arg);
|
||||
}
|
||||
|
||||
void file_opts_add() {
|
||||
|
||||
@@ -163,11 +163,11 @@ static bool incoming_listen_receive(const char *arg) {
|
||||
}
|
||||
|
||||
static bool incoming_listen_send(const char *arg) {
|
||||
return opts_add_send(incoming_add, send_flow, arg);
|
||||
return send_add(incoming_add, send_flow, arg);
|
||||
}
|
||||
|
||||
static bool incoming_listen_send_receive(const char *arg) {
|
||||
return opts_add_send(incoming_add, send_receive_flow, arg);
|
||||
return send_add(incoming_add, send_receive_flow, arg);
|
||||
}
|
||||
|
||||
void incoming_opts_add() {
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "send.h"
|
||||
|
||||
#include "opts.h"
|
||||
|
||||
#define OPTS_MAX 128
|
||||
@@ -22,21 +20,6 @@ static int opts_argc;
|
||||
static char **opts_argv;
|
||||
static opts_group opts_group_internal;
|
||||
|
||||
static struct serializer *opts_get_serializer(const 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;
|
||||
}
|
||||
|
||||
static void opts_print_usage() {
|
||||
fprintf(stderr,
|
||||
"Usage: %s [OPTION]...\n"
|
||||
@@ -111,11 +94,3 @@ char *opts_split(const char **arg, char delim) {
|
||||
*arg = split + 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool opts_add_send(bool (*next)(const char *, struct flow *, void *), struct flow *flow, const char *arg) {
|
||||
struct serializer *serializer = opts_get_serializer(&arg);
|
||||
if (!serializer) {
|
||||
return false;
|
||||
}
|
||||
return next(arg, flow, serializer);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
struct flow;
|
||||
|
||||
typedef bool (*opts_handler)(const char *);
|
||||
typedef char opts_group[1];
|
||||
|
||||
@@ -11,4 +9,3 @@ void opts_init(int, char *[]);
|
||||
void opts_add(const char *, const char *, opts_handler, opts_group);
|
||||
void opts_call(opts_group);
|
||||
char *opts_split(const char **, char);
|
||||
bool opts_add_send(bool (*)(const char *, struct flow *, void *), struct flow *, const char *);
|
||||
|
||||
@@ -173,11 +173,11 @@ static bool outgoing_connect_receive(const char *arg) {
|
||||
}
|
||||
|
||||
static bool outgoing_connect_send(const char *arg) {
|
||||
return opts_add_send(outgoing_add, send_flow, arg);
|
||||
return send_add(outgoing_add, send_flow, arg);
|
||||
}
|
||||
|
||||
static bool outgoing_connect_send_receive(const char *arg) {
|
||||
return opts_add_send(outgoing_add, send_receive_flow, arg);
|
||||
return send_add(outgoing_add, send_receive_flow, arg);
|
||||
}
|
||||
|
||||
void outgoing_opts_add() {
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "flow.h"
|
||||
#include "json.h"
|
||||
#include "log.h"
|
||||
#include "opts.h"
|
||||
#include "packet.h"
|
||||
#include "peer.h"
|
||||
#include "proto.h"
|
||||
@@ -124,6 +125,21 @@ static void send_new(int fd, void *passthrough, struct peer *on_close) {
|
||||
LOG(send->id, "New send connection: %s", serializer->name);
|
||||
}
|
||||
|
||||
static struct serializer *send_parse_format(const 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;
|
||||
}
|
||||
|
||||
void send_init() {
|
||||
assert(signal(SIGPIPE, SIG_IGN) != SIG_ERR);
|
||||
for (size_t i = 0; i < NUM_SERIALIZERS; i++) {
|
||||
@@ -190,3 +206,11 @@ void send_print_usage() {
|
||||
fprintf(stderr, "\t%s\n", serializers[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
bool send_add(bool (*next)(const char *, struct flow *, void *), struct flow *flow, const char *arg) {
|
||||
struct serializer *serializer = send_parse_format(&arg);
|
||||
if (!serializer) {
|
||||
return false;
|
||||
}
|
||||
return next(arg, flow, serializer);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
struct buf;
|
||||
struct flow;
|
||||
struct packet;
|
||||
@@ -10,4 +12,5 @@ void *send_get_serializer(const char *);
|
||||
void send_get_hello(struct buf **, void *);
|
||||
void send_write(struct packet *);
|
||||
void send_print_usage(void);
|
||||
bool send_add(bool (*)(const char *, struct flow *, void *), struct flow *, const char *);
|
||||
extern struct flow *send_flow;
|
||||
|
||||
Reference in New Issue
Block a user