From: Michael Tremer Date: Mon, 25 Jan 2021 12:59:01 +0000 (+0000) Subject: Parse mode from command line X-Git-Tag: 0.1.0~58 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d16996b1e40e9e4fedc508dff07f3429600d185e;p=fireperf.git Parse mode from command line Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 342253f..8a5413d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -60,6 +60,7 @@ bin_PROGRAMS = \ fireperf fireperf_SOURCES = \ + src/main.h \ src/main.c # ------------------------------------------------------------------------------ diff --git a/configure.ac b/configure.ac index 18c1890..b519fc6 100644 --- a/configure.ac +++ b/configure.ac @@ -63,7 +63,9 @@ AS_IF([test "x$enable_debug" = "xyes"], [ AC_CHECK_HEADERS_ONCE([ arpa/inet.h \ + getopt.h \ netinet/in.h \ + stdio.h \ string.h \ ]) diff --git a/src/main.c b/src/main.c index 083ada7..c31e2c4 100644 --- a/src/main.c +++ b/src/main.c @@ -18,6 +18,76 @@ # # #############################################################################*/ +#include +#include + +#include "main.h" + +static int parse_argv(int argc, char* argv[], struct fireperf_config* conf) { + static struct option long_options[] = { + {"client", required_argument, 0, 'c'}, + {"server", no_argument, 0, 's'}, + {0, 0, 0, 0}, + }; + + int option_index = 0; + int done = 0; + + while (!done) { + int c = getopt_long(argc, argv, "c:s", long_options, &option_index); + + // End + if (c == -1) + break; + + switch (c) { + case 0: + if (long_options[option_index].flag != 0) + break; + + printf("option %s", long_options[option_index].name); + + if (optarg) + printf(" with arg: %s", optarg); + + printf("\n"); + break; + + case '?': + // getopt_long already printed the error message + return 1; + + case 'c': + conf->mode = FIREPERF_MODE_CLIENT; + break; + + case 's': + conf->mode = FIREPERF_MODE_SERVER; + break; + + default: + done = 1; + break; + } + } + + return 0; +} + int main(int argc, char* argv[]) { + struct fireperf_config conf; + int r; + + // Parse command line + r = parse_argv(argc, argv, &conf); + if (r) + return r; + + // Check if a mode has been selected + if (!conf.mode) { + fprintf(stderr, "No mode selected\n"); + return 2; + } + return 0; } diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..22c900f --- /dev/null +++ b/src/main.h @@ -0,0 +1,27 @@ +/*############################################################################# +# # +# fireperf - A network benchmarking tool # +# Copyright (C) 2021 IPFire Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +struct fireperf_config { + enum { + FIREPERF_MODE_NONE = 0, + FIREPERF_MODE_CLIENT, + FIREPERF_MODE_SERVER, + } mode; +};