Files
adsb-tools/adsbus/opts.c

161 lines
3.9 KiB
C
Raw Normal View History

2016-02-28 13:15:12 -08:00
#include <assert.h>
2016-02-28 13:08:04 -08:00
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2016-02-28 15:53:55 -08:00
#include "exec.h"
2016-02-29 20:49:36 -08:00
#include "file.h"
2016-03-02 19:16:23 -08:00
#include "flow.h"
#include "incoming.h"
#include "outgoing.h"
#include "receive.h"
#include "send.h"
2016-03-03 18:28:34 -08:00
#include "send_receive.h"
#include "opts.h"
2016-03-07 11:26:25 -08:00
static char *opts_split(const char **arg, char delim) {
char *split = strchr(*arg, delim);
if (!split) {
return NULL;
}
char *ret = strndup(*arg, split - *arg);
*arg = split + 1;
return ret;
}
2016-03-07 11:26:25 -08:00
static bool opts_add_listen(const char *host_port, struct flow *flow, void *passthrough) {
char *host = opts_split(&host_port, '/');
if (host) {
incoming_new(host, host_port, flow, passthrough);
free(host);
} else {
incoming_new(NULL, host_port, flow, passthrough);
}
2016-03-03 18:28:34 -08:00
return true;
}
2016-03-07 11:26:25 -08:00
static bool opts_add_connect(const char *host_port, struct flow *flow, void *passthrough) {
2016-03-03 18:28:34 -08:00
char *host = opts_split(&host_port, '/');
if (!host) {
return false;
}
outgoing_new(host, host_port, flow, passthrough);
free(host);
return true;
}
2016-03-07 11:26:25 -08:00
static bool opts_add_file_write_int(const char *path, struct flow *flow, void *passthrough) {
2016-03-03 18:28:34 -08:00
file_write_new(path, flow, passthrough);
return true;
}
2016-03-07 11:26:25 -08:00
static bool opts_add_file_append_int(const char *path, struct flow *flow, void *passthrough) {
2016-03-03 18:28:34 -08:00
file_append_new(path, flow, passthrough);
return true;
}
2016-03-07 11:26:25 -08:00
static bool opts_add_exec(const char *cmd, struct flow *flow, void *passthrough) {
2016-03-03 18:28:34 -08:00
exec_new(cmd, flow, passthrough);
return true;
}
2016-03-07 11:26:25 -08:00
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;
}
2016-03-07 11:26:25 -08:00
static bool opts_add_send(bool (*next)(const char *, struct flow *, void *), struct flow *flow, const char *arg) {
2016-03-03 18:28:34 -08:00
struct serializer *serializer = opts_get_serializer(&arg);
if (!serializer) {
return false;
}
2016-03-03 18:28:34 -08:00
return next(arg, flow, serializer);
}
2016-03-07 11:26:25 -08:00
bool opts_add_connect_receive(const char *arg) {
2016-03-03 18:28:34 -08:00
return opts_add_connect(arg, receive_flow, NULL);
}
2016-03-07 11:26:25 -08:00
bool opts_add_connect_send(const char *arg) {
2016-03-03 18:28:34 -08:00
return opts_add_send(opts_add_connect, send_flow, arg);
}
2016-03-07 11:26:25 -08:00
bool opts_add_connect_send_receive(const char *arg) {
2016-03-03 18:28:34 -08:00
return opts_add_send(opts_add_connect, send_receive_flow, arg);
}
2016-03-07 11:26:25 -08:00
bool opts_add_listen_receive(const char *arg) {
2016-03-03 18:28:34 -08:00
return opts_add_listen(arg, receive_flow, NULL);
}
2016-03-07 11:26:25 -08:00
bool opts_add_listen_send(const char *arg) {
2016-03-03 18:28:34 -08:00
return opts_add_send(opts_add_listen, send_flow, arg);
}
2016-03-07 11:26:25 -08:00
bool opts_add_listen_send_receive(const char *arg) {
2016-03-03 18:28:34 -08:00
return opts_add_send(opts_add_listen, send_receive_flow, arg);
}
2016-03-07 11:26:25 -08:00
bool opts_add_file_read(const char *arg) {
2016-02-29 20:49:36 -08:00
file_read_new(arg, receive_flow, NULL);
2016-02-28 13:08:04 -08:00
return true;
}
2016-03-07 11:26:25 -08:00
bool opts_add_file_write(const char *arg) {
2016-03-03 18:28:34 -08:00
return opts_add_send(opts_add_file_write_int, send_flow, arg);
}
2016-02-29 20:49:36 -08:00
2016-03-07 11:26:25 -08:00
bool opts_add_file_write_read(const char *arg) {
2016-03-03 18:28:34 -08:00
return opts_add_send(opts_add_file_write_int, send_receive_flow, arg);
2016-02-28 13:08:04 -08:00
}
2016-03-07 11:26:25 -08:00
bool opts_add_file_append(const char *arg) {
2016-03-03 18:28:34 -08:00
return opts_add_send(opts_add_file_append_int, send_flow, arg);
}
2016-02-29 20:49:36 -08:00
2016-03-07 11:26:25 -08:00
bool opts_add_file_append_read(const char *arg) {
2016-03-03 18:28:34 -08:00
return opts_add_send(opts_add_file_append_int, send_receive_flow, arg);
2016-02-28 13:08:04 -08:00
}
2016-03-07 11:26:25 -08:00
bool opts_add_exec_receive(const char *arg) {
exec_new(arg, receive_flow, NULL);
2016-02-28 15:53:55 -08:00
return true;
}
2016-03-07 11:26:25 -08:00
bool opts_add_exec_send(const char *arg) {
2016-03-03 18:28:34 -08:00
return opts_add_send(opts_add_exec, send_flow, arg);
}
2016-02-28 15:53:55 -08:00
2016-03-07 11:26:25 -08:00
bool opts_add_exec_send_receive(const char *arg) {
2016-03-03 18:28:34 -08:00
return opts_add_send(opts_add_exec, send_receive_flow, arg);
2016-02-28 15:53:55 -08:00
}
2016-03-07 11:26:25 -08:00
bool opts_add_stdin(const char __attribute__((unused)) *arg) {
int fd = fcntl(STDIN_FILENO, F_DUPFD_CLOEXEC, 0);
assert(fd >= 0);
2016-03-02 22:25:56 -08:00
return flow_new_send_hello(fd, receive_flow, NULL, NULL);
}
2016-03-07 11:26:25 -08:00
bool opts_add_stdout(const char *arg) {
struct serializer *serializer = send_get_serializer(arg);
if (!serializer) {
return false;
}
int fd = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 0);
assert(fd >= 0);
2016-03-02 22:25:56 -08:00
return flow_new_send_hello(fd, send_flow, serializer, NULL);
}