Flag is just an empty option

This commit is contained in:
angt
2015-11-19 09:29:47 +01:00
parent 0c1e3a5f09
commit 540d0e2dff
3 changed files with 21 additions and 28 deletions

View File

@@ -581,20 +581,15 @@ int main (int argc, char **argv)
{ {
gt_set_signal(); gt_set_signal();
int listener = 0;
char *host = NULL; char *host = NULL;
char *port = "5000"; char *port = "5000";
char *dev = PACKAGE_NAME; char *dev = PACKAGE_NAME;
char *keyfile = NULL; char *keyfile = NULL;
char *congestion = NULL; char *congestion = NULL;
long buffer_size = GT_BUFFER_SIZE; long buffer_size = GT_BUFFER_SIZE;
int delay = 0;
int multiqueue = 0;
long ka_count = -1; long ka_count = -1;
long ka_idle = -1; long ka_idle = -1;
long ka_interval = -1; long ka_interval = -1;
int version = 0;
int debug = 0;
#ifdef TCP_INFO #ifdef TCP_INFO
struct { struct {
@@ -611,29 +606,32 @@ int main (int argc, char **argv)
}; };
struct option opts[] = { struct option opts[] = {
{ "listener", &listener, option_flag }, { "listener", NULL, option_option },
{ "host", &host, option_str }, { "host", &host, option_str },
{ "port", &port, option_str }, { "port", &port, option_str },
{ "dev", &dev, option_str }, { "dev", &dev, option_str },
{ "keyfile", &keyfile, option_str }, { "keyfile", &keyfile, option_str },
{ "congestion", &congestion, option_str }, { "congestion", &congestion, option_str },
{ "delay", &delay, option_flag }, { "delay", NULL, option_option },
{ "multiqueue", &multiqueue, option_flag }, { "multiqueue", NULL, option_option },
{ "keepalive", ka_opts, option_option }, { "keepalive", ka_opts, option_option },
{ "buffer-size", &buffer_size, option_long }, { "buffer-size", &buffer_size, option_long },
{ "debug", &debug, option_flag }, { "debug", NULL, option_option },
{ "version", &version, option_flag }, { "version", NULL, option_option },
{ NULL }, { NULL },
}; };
if (option(opts, argc, argv)) if (option(opts, argc, argv))
return 1; return 1;
if (version) { if (option_is_set(opts, "version")) {
gt_print(PACKAGE_STRING"\n"); gt_print(PACKAGE_STRING"\n");
return 0; 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"); int keepalive = option_is_set(opts, "keepalive");
if (buffer_size < 2048) { if (buffer_size < 2048) {
@@ -664,7 +662,7 @@ int main (int argc, char **argv)
struct netio tun = { .fd = -1 }; struct netio tun = { .fd = -1 };
struct netio sock = { .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) if (tun.fd==-1)
return 1; return 1;

View File

@@ -5,14 +5,6 @@
#include "option.h" #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) int option_str (void *data, int argc, char **argv)
{ {
if (argc<2 || !argv[1]) { 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) int option_option (void *data, int argc, char **argv)
{ {
if (!data)
return 0;
struct option *opts = (struct option *)data; struct option *opts = (struct option *)data;
for (int k=0; opts[k].name; k++) 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) static int option_usage (struct option *opts, int slen)
{ {
if (!opts)
return 0;
int len = slen; int len = slen;
for (int k=0; opts[k].name; k++) { 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); len += gt_print(" [%s", opts[k].name);
if (opts[k].call!=option_flag) { if (opts[k].call==option_option) {
if (opts[k].call==option_option) { len += option_usage((struct option *)opts[k].data, len);
len += option_usage((struct option *)opts[k].data, len); } else {
} else { len += gt_print(" ARG");
len += gt_print(" ARG");
}
} }
len += gt_print("]"); len += gt_print("]");

View File

@@ -7,10 +7,9 @@ struct option {
int set; int set;
}; };
int option_flag (void *, int, char **); int option_option (void *, int, char **);
int option_str (void *, int, char **); int option_str (void *, int, char **);
int option_long (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_is_set (struct option *, const char *);
int option (struct option *, int, char **); int option (struct option *, int, char **);