From aeff319a2433b3b7899c085ce2b954e883018df6 Mon Sep 17 00:00:00 2001 From: Patrick McHardy Date: Tue, 28 Sep 2010 21:18:54 +0200 Subject: [PATCH] example: add common FP command line option parser Signed-off-by: Patrick McHardy --- example/Makefile.in | 9 ++++--- example/common.h | 2 ++ example/fp-broadcast-page.c | 3 ++- example/fp-cc.c | 3 ++- example/fp-clms.c | 3 ++- example/fp-common.c | 50 +++++++++++++++++++++++++++++++++++++ example/fp-mm.c | 3 ++- 7 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 example/fp-common.c diff --git a/example/Makefile.in b/example/Makefile.in index e66f80e..5773f99 100644 --- a/example/Makefile.in +++ b/example/Makefile.in @@ -9,6 +9,7 @@ PROGRAMS += pp-wait-page destdir := usr/share/dect/examples common-obj += common.o event_ops.o keys.o dummy_ops.o debug.o +fp-common-obj += $(common-obj) fp-common.o pp-common-obj += $(common-obj) pp-common.o pp-auth.o ss-destdir := $(destdir) @@ -16,21 +17,21 @@ ss-obj += $(common-obj) ss-obj += ss.o fp-mm-destdir := $(destdir) -fp-mm-obj += $(common-obj) +fp-mm-obj += $(fp-common-obj) fp-mm-obj += fp-mm.o fp-cc-destdir := $(destdir) -fp-cc-obj += $(common-obj) +fp-cc-obj += $(fp-common-obj) fp-cc-obj += audio.o fp-cc-obj += fp-cc.o fp-cc-ldflags += -lSDL fp-clms-destdir := $(destdir) -fp-clms-obj += $(common-obj) +fp-clms-obj += $(fp-common-obj) fp-clms-obj += fp-clms.o fp-broadcast-page-destdir := $(destdir) -fp-broadcast-page-obj += $(common-obj) +fp-broadcast-page-obj += $(fp-common-obj) fp-broadcast-page-obj += fp-broadcast-page.o pp-access-rights-destdir := $(destdir) diff --git a/example/common.h b/example/common.h index 14865cd..f827820 100644 --- a/example/common.h +++ b/example/common.h @@ -26,6 +26,8 @@ extern int dect_write_uak(const struct dect_ipui *ipui, extern int dect_read_uak(const struct dect_ipui *ipui, uint8_t uak[DECT_AUTH_KEY_LEN]); +extern void dect_fp_common_options(int argc, char **argv); + extern void dect_pp_auth_init(struct dect_ops *ops, const struct dect_ipui *ipui); extern void dect_pp_common_init(struct dect_ops *ops, const char *cluster, diff --git a/example/fp-broadcast-page.c b/example/fp-broadcast-page.c index 6c5151c..6a5dbc5 100644 --- a/example/fp-broadcast-page.c +++ b/example/fp-broadcast-page.c @@ -9,7 +9,8 @@ static struct dect_ops ops; int main(int argc, char **argv) { - dect_common_init(&ops, argv[1]); + dect_fp_common_options(argc, argv); + dect_common_init(&ops, cluster); dect_lce_group_ring_req(dh, 0); dect_event_loop(); diff --git a/example/fp-cc.c b/example/fp-cc.c index 8143733..7307bc1 100644 --- a/example/fp-cc.c +++ b/example/fp-cc.c @@ -216,7 +216,8 @@ static struct dect_ops ops = { int main(int argc, char **argv) { - dect_common_init(&ops, argv[1]); + dect_fp_common_options(argc, argv); + dect_common_init(&ops, cluster); dect_open_call(dh, &ipui); diff --git a/example/fp-clms.c b/example/fp-clms.c index f677cf8..b8d115e 100644 --- a/example/fp-clms.c +++ b/example/fp-clms.c @@ -38,7 +38,8 @@ static struct dect_ops ops; int main(int argc, char **argv) { - dect_common_init(&ops, argv[1]); + dect_fp_common_options(argc, argv); + dect_common_init(&ops, cluster); dect_invoke_clms(dh); diff --git a/example/fp-common.c b/example/fp-common.c new file mode 100644 index 0000000..de9e479 --- /dev/null +++ b/example/fp-common.c @@ -0,0 +1,50 @@ +/* + * DECT PP common functions + * + * Copyright (c) 2010 Patrick McHardy + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include "common.h" + +enum { + OPT_CLUSTER, + OPT_HELP, +}; + +static const struct option options[] = { + { .name = "cluster", .has_arg = true, .flag = NULL, .val = OPT_CLUSTER }, + { .name = "help", .has_arg = false, .flag = NULL, .val = OPT_HELP }, + + { }, +}; + +void dect_fp_common_options(int argc, char **argv) +{ + int optidx = 0, c; + + for (;;) { + c = getopt_long(argc, argv, "c:h", options, &optidx); + if (c == -1) + break; + + switch (c) { + case OPT_CLUSTER: + cluster = optarg; + break; + case OPT_HELP: + printf("%s: [ -c/--cluster NAME ] [ -h/--help ]\n", + argv[0]); + exit(0); + case '?': + exit(1); + } + } +} diff --git a/example/fp-mm.c b/example/fp-mm.c index cce8a3f..98b2091 100644 --- a/example/fp-mm.c +++ b/example/fp-mm.c @@ -219,7 +219,8 @@ int main(int argc, char **argv) if (rand_fd < 0) pexit("open /dev/urandom"); - dect_common_init(&ops, argv[1]); + dect_fp_common_options(argc, argv); + dect_common_init(&ops, cluster); dect_event_loop();