From 540d0e2dff77197fdfab72dbb7001fe82550d2f2 Mon Sep 17 00:00:00 2001 From: angt Date: Thu, 19 Nov 2015 09:29:47 +0100 Subject: [PATCH] Flag is just an empty option --- src/main.c | 22 ++++++++++------------ src/option.c | 24 ++++++++++-------------- src/option.h | 3 +-- 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/main.c b/src/main.c index 48f02cb..21e89b4 100644 --- a/src/main.c +++ b/src/main.c @@ -581,20 +581,15 @@ int main (int argc, char **argv) { gt_set_signal(); - int listener = 0; char *host = NULL; char *port = "5000"; char *dev = PACKAGE_NAME; char *keyfile = NULL; char *congestion = NULL; long buffer_size = GT_BUFFER_SIZE; - int delay = 0; - int multiqueue = 0; long ka_count = -1; long ka_idle = -1; long ka_interval = -1; - int version = 0; - int debug = 0; #ifdef TCP_INFO struct { @@ -611,29 +606,32 @@ int main (int argc, char **argv) }; struct option opts[] = { - { "listener", &listener, option_flag }, + { "listener", NULL, option_option }, { "host", &host, option_str }, { "port", &port, option_str }, { "dev", &dev, option_str }, { "keyfile", &keyfile, option_str }, { "congestion", &congestion, option_str }, - { "delay", &delay, option_flag }, - { "multiqueue", &multiqueue, option_flag }, + { "delay", NULL, option_option }, + { "multiqueue", NULL, option_option }, { "keepalive", ka_opts, option_option }, { "buffer-size", &buffer_size, option_long }, - { "debug", &debug, option_flag }, - { "version", &version, option_flag }, + { "debug", NULL, option_option }, + { "version", NULL, option_option }, { NULL }, }; if (option(opts, argc, argv)) return 1; - if (version) { + if (option_is_set(opts, "version")) { gt_print(PACKAGE_STRING"\n"); return 0; } + int listener = option_is_set(opts, "listener"); + int delay = option_is_set(opts, "delay"); + int debug = option_is_set(opts, "debug"); int keepalive = option_is_set(opts, "keepalive"); if (buffer_size < 2048) { @@ -664,7 +662,7 @@ int main (int argc, char **argv) struct netio tun = { .fd = -1 }; struct netio sock = { .fd = -1 }; - tun.fd = tun_create(dev, multiqueue); + tun.fd = tun_create(dev, option_is_set(opts, "multiqueue")); if (tun.fd==-1) return 1; diff --git a/src/option.c b/src/option.c index 70d58fb..4834608 100644 --- a/src/option.c +++ b/src/option.c @@ -5,14 +5,6 @@ #include "option.h" -int option_flag (void *data, _unused_ int argc, _unused_ char **argv) -{ - const int one = 1; - byte_cpy(data, &one, sizeof(one)); - - return 0; -} - int option_str (void *data, int argc, char **argv) { if (argc<2 || !argv[1]) { @@ -58,6 +50,9 @@ int option_is_set (struct option *opts, const char *name) int option_option (void *data, int argc, char **argv) { + if (!data) + return 0; + struct option *opts = (struct option *)data; for (int k=0; opts[k].name; k++) @@ -96,6 +91,9 @@ int option_option (void *data, int argc, char **argv) static int option_usage (struct option *opts, int slen) { + if (!opts) + return 0; + int len = slen; for (int k=0; opts[k].name; k++) { @@ -106,12 +104,10 @@ static int option_usage (struct option *opts, int slen) len += gt_print(" [%s", opts[k].name); - if (opts[k].call!=option_flag) { - if (opts[k].call==option_option) { - len += option_usage((struct option *)opts[k].data, len); - } else { - len += gt_print(" ARG"); - } + if (opts[k].call==option_option) { + len += option_usage((struct option *)opts[k].data, len); + } else { + len += gt_print(" ARG"); } len += gt_print("]"); diff --git a/src/option.h b/src/option.h index f048bb7..457ee8b 100644 --- a/src/option.h +++ b/src/option.h @@ -7,10 +7,9 @@ struct option { int set; }; -int option_flag (void *, int, char **); +int option_option (void *, int, char **); int option_str (void *, int, char **); int option_long (void *, int, char **); -int option_option (void *, int, char **); int option_is_set (struct option *, const char *); int option (struct option *, int, char **);